【问题标题】:Why Javascript constructor doesn't return [closed]为什么Javascript构造函数不返回[关闭]
【发布时间】:2019-02-26 15:31:41
【问题描述】:

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

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

【问题讨论】:

  • 您尝试做的事情是晦涩难懂的。你能解释一下你的意图(并将你的代码减少到minimal reproducible example)吗?

标签: javascript html oop prototype


【解决方案1】:

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

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

您似乎正在尝试创建重载的构造函数?! javascript 不支持函数重载!

【讨论】:

    【解决方案2】:

    我可以确认,正如@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”的警报。

    【讨论】:

      【解决方案3】:

      你在函数中没有任何返回语句

      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);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多