javascript:对象继承无法理解

时间:2014-12-09 18:58:13

标签: javascript inheritance

我在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());

1 个答案:

答案 0 :(得分:-1)

你需要写 -

var newOnj = new subGadget();