这工作设置原型:使用Javascript - 从内部功能
function Bird(name){
Animal.call(this,name);
this.speak = function(){
console.log("Tweeet!");
}
}
Bird.prototype.constructor = Animal;
这将引发 “无法设置属性未定义的构造函数”“
function Bird(name){
Animal.call(this,name);
this.speak = function(){
console.log("Tweeet!");
}
this.prototype.constructor = Animal;
}
为什么会是这样?在第二个例子中,这应该鸟因为我已经调用的函数与新,所以我应该能够设置这原型。我在这里错过了什么?
'this'内部功能 - 对象,因此它不具有原型财产,但如果你将'this'改为'Bird'都应该可以工作 – Grundy 2015-03-02 10:08:41
只是回答我自己的问题,现在我理解JavaScript更好一些。在第二个示例中,'this'不是'Bird' - 'this'是已创建并传递给Bird函数的新对象。 – wagster 2015-09-30 08:14:50