我有这个代码:
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());
Run Code Online (Sandbox Code Playgroud)
输出告诉我a没有名为getA()的方法
我假设在SecondClass的构造函数中执行this.prototype = new MyClass()会导致它从MyClass的inhert方法?
我确信有更好的方法可以做到这一点,但我试图理解prototype关键字的行为.
javascript ×1