0

This is the code I have

class Parent {
  async A(part, id) {
  }

  async B(part, id) {
    await this.A(part, id)
  }
}

class Child extends Parent {
  async A(id) {
  }

  async B(id) {
    await super.B("part1", id);
    // ...
  }
}

// let's assume I'm in async context
const child = new Child();
await child.B();

So what is happening here is the Child.B is calling Parent.B, and Parent.B is calling Child.A (which is expected).

I want to know if there is a way for me to call Parent.B > Parent.A.

I realize that perhaps I need to rename the methods, but I wanted to know if there is a way for this.

Kat Lim Ruiz
  • 2,425
  • 2
  • 26
  • 32
  • 1
    you certainly can call `super.A`... – Daniel A. White Jan 17 '22 at 15:18
  • 1
    @DanielA.White No, that won't work. `Parent.A` has no `super`. – Bergi Jan 17 '22 at 15:19
  • Have a look at [this](https://stackoverflow.com/questions/25060621/prototype-chain-call-super-method-over-multiple-levels/25060720#25060720) or [that](https://stackoverflow.com/questions/67879135/js-why-would-child-class-method-call-parent-class-method) – Bergi Jan 17 '22 at 15:23
  • Another related question: https://stackoverflow.com/questions/62681646/how-to-call-the-base-class-constructor-in-multi-level-inheritance – Bergi Jan 17 '22 at 15:33

1 Answers1

2

If you want to call a method of a specific class instead of looking it up on the instance (on this) or your own prototype (trough super), reference that method directly and .call it on the instance:

class Parent {
  async A(part, id) {
  }

  async B(part, id) {
    await Parent.prototype.A.call(this, part, id)
  }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375