【发布时间】: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());
【问题讨论】: