Reputation: 113365
Having two classes:
class Parent {
foo () {
console.log("foo parent");
}
}
class Child extend Parent {
foo () {
console.log("foo child");
// Is there a better way than this?
Parent.prototype.foo.call(this);
}
}
Instead of Parent.prototype.foo.call(this)
–is there a better way?
To call the parent constructor I use super
. I am thinking if there is a similar way to call the original function (from parent) which was overridden in the child class.
Upvotes: 0
Views: 102
Reputation: 64923
super
can be also used with regular methods:
class A {
foo() {
console.log("hello world");
}
}
class B extends A {
foo() {
super.foo();
}
}
// outputs "hello world" to the console log
new B().foo();
Upvotes: 4