Can the class of ES6 specify the method that invokes the parent class itself in the parent class?

class A {
  x() {  }
  y() { 
    Object.getPrototypeOf(Object.getPrototypeOf(this)).x.call(this) //
  }
}

class B extends A {
  x() {
    //  
  }
  y() { 
    super.x()
  }
}

const b = new B()
b.y()

I know that the method of the parent class can be called with super.x () in the subclass, but what if the method itself is defined in the parent class?
at present, using the prototype chain can be solved temporarily, but if there is still a problem with new A directly, and it feels strange to use class and prototype, is there a more elegant way to write it?

Mar.23,2022

suppose An is a parent class and B is a subclass.
Class A has x and y methods and Class B also has x and y methods. Obviously, methods of Class B will override methods of the same name in Class A
if you want to call methods of the same name in Class An in Class B, you can do so

.
const b = new B()
const x = A.x
x.call(b)

this implements calling the x method of parent class An in the class B instance


if you want to call the x method of A, you can call it through this.x ().

if you are thinking about calling the x method overridden by B in A, then you need to reconsider your inheritance chain, which is not a legitimate inheritance usage.

Menu