我有这段代码:
var MyClass = function(b) {
this.a = b;
this.getA = function() {
return that.a;
}
}
var SecondClass = function(b) {
this.prototype = new MyClass(b);
this.getB = function() {
return 6;
}
}
var a = new SecondClass(2);
console.log(a.getA());
输出告诉我a没有名为getA()的方法。
我假设在SecondClass构造函数内部执行this.prototype = new MyClass()将使它继承MyClass的方法?
我确定有更好的方法来做这件事,但我想要理解原型关键字的行为。
return that.a;
应该改为return this.a;
。 - Felix Kling