我想创建继承自父类(super class) A 的子类 B。 我的代码在这里:
function A(){
this.x = 1;
}
B.prototype = new A;
function B(){
A.call(this);
this.y = 2;
}
b = new B;
Console.log(b.x + " " + b.y );
运行时,显示B未定义。
最佳答案
您必须在尝试访问其原型(prototype)之前定义 B 构造函数:
function A(){
this.x = 1;
}
function B(){
A.call(this);
this.y = 2;
}
B.prototype = new A;
b = new B;
console.log(b.x + " " + b.y ); // outputs "1 2"
关于JavaScript 对象子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1204359/