我需要重写Parent方法并使用NodeJs调用Grandparent方法。可能吗?
例如:A
和B
是库类。
class A {
printFunction() {
console.log('Hello');
}
}
class B extends A {
printFunction() {
console.log('World');
}
runFunction() {
console.log('Running');
}
}
现在我有一个类C
扩展类B
。
class C extends B {
// can i call class A printFunction here?
}
在C级,我可以调用A的printFunction
吗?
你可以使用Function.prototype.call
调用确切的函数:
class C extends B {
callA() {
A.prototype.printFunction.call(this);
}
}
不,不是在C类扩展B类时,因为你已经覆盖了B类中的printFunction()
。从C类的角度来看,它并不关心A类对printFunction()
的定义,因为它现在已经被修改了。
唯一的方法是让C类扩展A类,并重新定义B类所需的任何函数。