为什么Javascript构造函数不返回

时间:2019-02-26 15:31:41

标签: javascript html oop prototype

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

//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>

3 个答案:

答案 0 :(得分: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);

这会发出一个警告,说“ ali”。

答案 1 :(得分: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);

答案 2 :(得分:0)

如果您指的是 SELECT TRY_CAST(column1 as bigint) as column1 FROM table Where TRY_CAST (column1 as bigint) > 90 order by column1; 确实返回实例!它不会返回具有属性var obj2 = new Creatures("ali",true);name的新Creatures实例,因为它们不是构造函数的参数,也不是构造函数中设置的属性(顶级参数):

alive

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