How to call grandparent method in Nodejs

Viewed 470

I need to override Parent method and call Grandparent method with NodeJs. Is it possible?

For example: A and B are library classes.

class A {
   printFunction() {
       console.log('Hello');
   }
}

class B extends A {
   printFunction() {
       console.log('World');
   }
   runFunction() {
       console.log('Running');
   }
}

Now I have a class C which extends class B.

class C extends B {
     // can i call class A printFunction here?
}

In the class C, can i call printFunction of A?

2 Answers

You can call the exact function using Function.prototype.call:

class C extends B {
  callA() {
    A.prototype.printFunction.call(this);
  }
}

No, not while class C extends class B, because you've already overridden the printFunction() in class B. From class C's point of view, it doesn't care what class A's definition of printFunction() is because that has now been modified.

The only way to do this would be to have class C extend class A, and redefine any function you require from class B.

Related