我正在尝试处理一些 javascript 继承示例,但我遇到了这个问题:
function Animal(){}
Animal.prototype.type = "animal";
Animal.prototype.speak = function(){ console.log( "I'm a " + this.type +
". I can't really talk ;)" ); }
function Dog(){}
function F(){}
F.prototype = Animal.prototype;
Dog.prototype = new F();
Dog.prototype.constructor = Dog;
Dog.prototype.type = "Dog";
Dog._super = Animal.prototype;
Dog.woof = function(){ console.log( "Woof!" ); _super.speak(); }
var rover = new Dog();
rover.woof();
我得到了这个,我不知道为什么:
TypeError: Object #<Dog> has no method 'woof'
我知道我可以将未找到的方法放入构造函数中,但我试图通过原型修改来做到这一点。我在这里做错了什么?