【问题标题】:javascript: object inheritance is not able understandjavascript:对象继承无法理解
【发布时间】:2015-02-07 19:16:33
【问题描述】:

我在 JavaScript 中工作继承,在这里,创建了两个对象。在下面的代码中,当尝试以alert(newOnj.whatAreYou()); 发出警报时,现在它工作正常。但是,当我尝试提醒alert(newOnj.whatAreYounow()); 时,现在出现'newOnj.whatAreYounow is not a function' 的错误。实际上,已将父类继承为子类subGadget.prototype = new Gadget();。我haven't 在这里理解的是什么?请

function Gadget() { 
   this.name = "Alex"; 
   this.color = "red"; 
   this.whatAreYou = function(){ 
     return 'I am a ' + this.color + ' ' + this.name; 
   }
}
  function subGadget() { 
     this.whatAreYounow = function(){ 
     return 'Now, I am a ' + this.color + ' ' + this.name; 
   }
}

  subGadget.prototype = new Gadget();

  var newOnj = new Gadget();

 alert(newOnj.whatAreYounow());

【问题讨论】:

    标签: javascript inheritance


    【解决方案1】:

    你需要写 -

    var newOnj = new subGadget();
    

    【讨论】:

    猜你喜欢
    • 2015-07-10
    • 2019-01-30
    • 1970-01-01
    • 2017-01-20
    • 2017-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多