What is the problem with the inheritance case code of "getting started with ECMAScript 6" Class?

< H2 > "getting started with ECMAScript 6"-in the inheritance chapter of sharp-sharp Class < / H2 >

the original case code is as follows:

class A {
  constructor() {
    this.x = 1;
  }
}

class B extends A {
  constructor() {
    super();
    this.x = 2;
    super.x = 3;
    console.log(super.x); // undefined
    console.log(this.x); // 3
  }
}

let b = new B();

content links

the original book"s interpretation of this code

in the above code, the super.x assignment is 3, which is equivalent to the this.x assignment of 3. When reading super.x, it reads A.prototype.x, so it returns undefined.

but the actual environment test secondary code, console.log (this.x) this statement actually outputs 2 , not 3.
is the description in the book wrong, or do I understand it wrong?

Mar.25,2021

Note: if you are performing this paragraph under Babel transcoding, the result is really not 3.
you can take a look at my article: Babel transcoding matters needing attention for" super "processing

Menu