为什么Javascript构造函数不返回[关闭]

问题描述 投票:1回答:3

我有个问题。当我从一个对象实例时,它不返回构造函数的值。 .................................................. .................................................. .................................................. .......... ........................................ .................................................. .................................................. ....................

//Main Branch
var Creatures = function () {
    function Creatures(name,alive) {
        this.name = name;
        this.alive = alive;
    }
    function Creatuers(name,alive,sleep) {
        this.name = name;
        this.alive = alive;
        this.sleep = sleep;
    }
};

var Beast = function () {
    function breathe() {
        return true;
    }
    this.__proto__ = new Creatures;
};
var Mammals = function () {
    this.numberOfBeads = "i am numberOfBeads from Mammals";
    this.__proto__ = new Beast;
};


//Mammals Branch
var Humans = function () {
    function thinking(){
        return true;
    }
    this.love = "i am love from humans";
    this.__proto__ = new Mammals;
};
var Animals = function () {
    this.instinct = "i am Animals from Animals";
    this.__proto__ = new Mammals;
};
var Plants = function () {
    this.color = "i am color from Plants";
    function grown() {
        return true;
    }
    this.__proto__ = new Creatures;
};
var Trees = function () {
    this.height = "i am height from Trees";
    this.__proto__ = new Plants;
};
var Flowers = function () {
    this.smell = "i am smell from Flowers";
    this.__proto__ = new Plants;
};
var Grasses = function () {
    this.color = "i am color from Grasses";
    this.__proto__ = new Plants;
};
var obj2 = new Creatures("ali",true);
alert(obj2.name);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>

<script src="sampleObject1.js"></script>
<script src="sampleObject2.js"></script>
</body>
</html>
javascript html oop prototype
3个回答
0
投票

如果你指的是var obj2 = new Creatures("ali",true);它会返回一个实例!它不返回具有属性namealive的新Creatures实例,因为它们不是构造函数的参数,也不是构造函数中设置的属性(顶级一):

var Creatures = function () {
    ...
};

你似乎要做的是创建重载的构造函数?! javascript不支持函数重载!


0
投票

我可以确认@Denys Seguret说,某些事情似乎相当模糊......

无论如何,你得到未定义,因为函数构造函数返回一个对象,该对象具有你在该函数构造函数中附加的属性。

在您的情况下,您的功能是:

var Creatures = function () {
    function Creatures(name,alive) {
        this.name = name;
        this.alive = alive;
    }
    function Creatures(name,alive,sleep) {
        this.name = name;
        this.alive = alive;
        this.sleep = sleep;
    }
};

这是一个函数表达式,在其范围内有两个具有相同名称的函数(第二个函数将覆盖第一个,尽管第一个函数没用)。

如您所见,您没有为将从其隐式返回的对象设置任何类型的属性。

我让你的例子只是用第二个函数声明替换它:

function Creatures(name,alive,sleep) {
  this.name = name;
  this.alive = alive;
  this.sleep = sleep;
}

var obj2 = new Creatures("ali", true);
alert(obj2.name);

这会发出一个警告“阿里”。


0
投票

你在函数中没有任何return语句

var Creatures = function () {
    function Creatures(name,alive) {
        this.name = name;
        this.alive = alive;
    }
    function Creatuers(name,alive,sleep) {
        this.name = name;
        this.alive = alive;
        this.sleep = sleep;
    }
};

您可能要做的是委托对象的实例化,根据参数(可能还有一些业务逻辑)选择合适的构造函数。注意javascript中没有方法重载,所以你需要为内部函数使用不同的命名。

var Creatures = function (...params) {
    function Creatures1(name,alive) {
        this.name = name;
        this.alive = alive;
    }
    function Creatures2(name,alive,sleep) {
        this.name = name;
        this.alive = alive;
        this.sleep = sleep;
    }
    return params.length === 2 ? new Creatures1(...params) : new Creatures2(...params);
};

var obj2 = Creatures("ali",true);
alert(obj2.name);
© www.soinside.com 2019 - 2024. All rights reserved.