在ES6,“超级”可以调用基类,如:“超级”可以调用基类的方法和属性
class A{
constructor(){
this.p = 2;
}
initF(){
return this.p
}
}
class B extends A{
constructor(){
super();
console.log(super.p); // undefined
console.log(super.initF()); // 2
}
ff(){
console.log(super.initF()); // 2
}
}
new B().ff();
很明显,我们才可以调用基类(A)的方法,我们不能称之为原型。为什么不能通过“超级”访问基类的原型?
派生类得到的所有财产从基类。所以你可以使用'this'来访问我所有的属性基类。 console.log(this.p); –
_“为什么不能通过”super“访问基类原型”_ _其实,这就是你在你的例子中所做的。 – zeroflagL