这是在看阮一峰老师的es6中关于类遇到的问题。代码如下,虽然答大体意思我知道,不过不是太能理解super.x输出为undefined。
class A {
constructor() {
this.x = 1;
}
}
class B extends A {
constructor() {
super();
this.x = 2;
super.x = 3; //super.x.call(this),即为this.x。但属性没有call方法
//super.x即为A.prototype.x,A中的x为实例的属性,而实例的属性和方法都无法在原型上访问到(能懂)
console.log(super.x); // undefined 为什么这里读到的一定是A.prototype.x,而不会是this.x???
console.log(this.x); // 3
}
}
let b = new B();
翻翻红宝书就知道了
就是 call , apply 的应用罢了