如何专门化 Javascript 类?
我正在研究 JavaScript 类继承(我正在使用 node.js)。我得到子类实例的“未定义”值。这是我的示例:
我定义了一个 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);
我使用其构造函数创建了 Squirrel 的 squirrel 实例,并将一些值传递给构造函数,这按预期工作。当我尝试使用其构造函数创建子类 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!
I was playing with JavaScript class inheritance (I am using node.js). I get "undefined" values for instances of the child class. Here my example:
I define a Squirrel class and I want to specialize this class in a KillerSquirrel child class. I want create instances of both the Squirrel and the KillerSquirrel classes.
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);
I create a squirrel instance of Squirrel using its constructor and passing some values to the constructor and this works as expected. When I try to create an instance of child class KillerSquirrel using its constructor and passing some values to it, the killer squirrel instance has "undefined properties".
see:
$ 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
子类构造函数应该通过特殊构造(apply 或 call)手动调用超类构造函数,如下所示:
或者
尽管对于这种情况(当参数相同时),第一种形式是首选。
Subclass constructor should call superclass constructor manually by special construct (apply or call), like this:
or
though for this case (when the arguments are the same) the first form is preferred.