Ionică Bizău
Ionică Bizău

Reputation: 113365

Call a function that was overridden in parent class

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

Answers (1)

Matías Fidemraizer
Matías Fidemraizer

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

Related Questions