我在玩JavaScript类继承(我使用node.js)。我得到了子类的实例的“未定义”值。这里我的例子:如何专门化Javascript类?
我定义了一个Squirrel类,我想在KillerSquirrel子类中专门化这个类。我想创建Squirrel和KillerSquirrel类的实例。
function Squirrel(name, color) {
this.name = name;
this.color = color;
console.log("A new " + this.color + " squirrel named " + this.name + " is born!!!");
};
// add a new method called speak()
Squirrel.prototype.speak = function(text) {
console.log(this.name + " squirrel says: '" + text + "'");
};
// specialize the Squirrel class by creating a new KillerSquirrel constructor
function KillerSquirrel(name, color) {
this.soul = 'very bad';
console.log("Bhrrr ... a new " + this.color + " killer squirrel named " + this.name + " is born!!!");
}
KillerSquirrel.prototype.__proto__ = Squirrel.prototype;
// add kill method
KillerSquirrel.prototype.kill = function(obj){
console.log(this.name + " squirrel killed " + obj.name + " squirrel");
}
// replace the speak() method
KillerSquirrel.prototype.speak = function(text) {
console.log(this.name + " squirrel says: 'Grhhh! " + text + "' Grhhh!");
};
var squirrel = new Squirrel("Gummy", "brown");
squirrel.speak("My name is " + squirrel.name);
var killer = new KillerSquirrel("Mr.Hide", "black");
killer.speak("My name is " + killer.name);
我使用它的构造函数创建松鼠的松鼠实例,并将一些值传递给构造函数,并按预期工作。当我尝试使用它的构造函数创建一个KillerSquirrel类的实例并传递一些值时,杀手松鼠实例具有“未定义的属性”。
看到:(申请或电话)
$ node killersquirrel.js
A new brown squirrel named Gummy is born!!!
Gummy squirrel says: 'My name is Gummy'
Bhrrr ... a new undefined killer squirrel named undefined is born!!!
undefined squirrel says: 'Grhhh! My name is undefined' Grhhh!
_all_松鼠是杀手松鼠 - 没有必要继承它们! ;-) – Alnitak
不确定要理解评论。谢谢 – kalise
@kalise我认为评论是一个笑话。 –