Javascript es6 super doesn't understand.

I am a novice. Recently, I read the chapter of inheritance of class in teacher Ruan Yifeng"s es6 . I don"t understand some concepts of super . The details are as follows:
there is a code like this in teacher Ruan Yifeng"s blog:

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

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

let b = new B();

that"s what teacher Ruan Yifeng said

because this points to the subclass instance, if you assign a value to an attribute through super , then super is this , and the assigned attribute becomes the property of the subclass instance.
in the above code, super.x is assigned 3 , which is equivalent to 3 assigned to this.x .

I don"t understand why using super.x to assign a value equates to this.x ,

.

my understanding is:
according to what teacher Ruan Yifeng said earlier

super as an object, in a normal method, point to the prototype object of the parent class

so the super.x = 3 here should actually be A.prototype.x = 3 , so there should be no effect on the subclass B , so the result should be 2
, but the result from the browser is the same as that of teacher Ruan 3 , so how to understand

I hope I can have a clear answer from my seniors. Thank you.


the subject reads the book more carefully

in the second case, when super is an object, in a normal method, it points to the prototype object of the parent class; in a static method, it points to the parent class.
ES6 states that when a method of a parent class is called through super in a normal method of a subclass, the this inside the method points to the current instance of the subclass.

these two sentences are linked together and are not contradictory! It means:

means:

when calling the method of the parent class through super in the normal method of the subclass

super points to the prototype object of the parent class, but the this in the method of the parent class points to the current subclass instance.

super.x = 3;

here super points to the parent class's prototype object, yes!

but the underlying layer does not execute

directly.
A.prototype.x = 3

instead transforms the underlying this , with a final effect similar to:

this = 3

Code validation:

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

class B extends A {
  constructor() {
    super()
    this.x = 2
    Object.defineProperty(A.prototype, 'x', {
      set: function(val) {
        console.log(this, val)
         //B {x: 2} 3,superB {x: 2}this
      }
    })
    super.x = 3
    console.log(this.x) // 3
  }
}

let b = new B()

if you look at the example of a function call given by teacher Ruan, this example is even clearer, because the function can use call

.

clipboard.png

:


super.print() super.print.call(this)this

:

thissupersuperthis

supersuperthis

super super thissuper


super()prototypeclass super.x = 3 3thissuper.x


super() this, superthissupersuper


322


:

personal understanding is the same as the subject: super points to the prototype object of the parent class, so assigning values on super.x does not affect the this, pointing to the instance object, so it should still be 2, and the actual running result is also 2.
Why do some people get 2 and others get 3? it may be the reason for the es version, which needs to be studied carefully, and the gods who hope to understand will not hesitate to give us advice.


is only for personal understanding, for reference only. Teacher Ruan Yifeng said:
1, when calling the method of the parent class through super in the common method of the subclass, the this inside the method points to the current instance of the subclass.
2. I understand that in super.x=3, it is not assigned directly, but the set function in the super object is called, in which the this is changed to point to the current instance.
3. Super does not have set/get, itself, but he inherits object's get/set method


because this points to the subclass instance, if you assign a value to an attribute through super , then super is this , and the assigned attribute becomes the property of the subclass instance.
in the above code, super.x is assigned 3 , which is equivalent to 3 assigned to this.x .
class A {
  constructor() {
    this.x = 1;
  }
}

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

let b = new B();

I don't understand the concrete implementation of super , but add a line of code according to this logic, and the result is obviously not valid. Weak doubt that this is not a counter-guess through the phenomenon, is it?

Menu