I
I
IkaR492019-07-11 19:05:16
JavaScript
IkaR49, 2019-07-11 19:05:16

How to access the overridden parent method of the parent (grandfather)?

There are the following classes:

class A {
    foo() { alert("A"); }
}
class B extends A {
    foo() { alert("B"); }
}
class C extends B {
    foo() { alert("C"); }
    bar() { /* Здесь я хочу вызвать A::foo() */ }
}

If I write this.foo() in the bar() method , I get access to C::foo() . If super.foo() , then I will get access to B::foo( ). But how do you get access to A::foo() from C::bar() ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
grinat, 2019-07-11
@grinat

Through call / aply, A.prototype.foo.call(this), but no one does such nested classes in js because inheritance is prototype and works slowly

A
Anton Shvets, 2019-07-11
@Xuxicheta

class A {
    foo() { alert("A"); }
}
class B extends A {
    foo() { alert("B"); }
}
class C extends B {
    foo() { alert("C"); }
    bar() { Reflect.getPrototypeOf(Reflect.getPrototypeOf(Reflect.getPrototypeOf(this))).foo(); }
}

const c = new C();

c.bar();

or
this.__proto__.__proto__.__proto__.foo.call(this)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question