Reputation: 91
I want this.method2()
call of base
to execute base.method2()
while it actually executes derived.method2()
(I understand the idea behind the behavior), is it possible to achieve this without methods renaming and what is the best practice here?
const base = class {
method() {
this.method2();
}
method2() {
console.error("base.method2");
}
}
const derived = new class extends base {
method() {
super.method();
}
method2() {
console.error("derived.method2");
}
}
derived.method(); // want 'base.method2' here
Upvotes: 2
Views: 36
Reputation: 237855
You can do this with call
.
method() {
base.prototype.method2.call(this);
}
In this case you don't technically need to use call
and supply the this
value, because you don't use it, but it is the best way to create the call you want so it works in all circumstances.
Incidentally, I don't understand what you are seeking to achieve with the const base = class {}
statement. A class declaration would be much less surprising: class base {}
.
Upvotes: 2