Is there any way to call super.super.methodName
in TypeScript. I want to avoid calling super.methodName
, but I want to call the 2nd ancestor's methodName
method.
Thanks.
The super keyword is used to call the constructor of its parent class to access the parent's properties and methods.
You can call the base class constructor from the child class by using the super() which will execute the constructor of the base class.
super is ES6 syntax that cannot be used outside the method where it's used. Given there is Foo class that extends Bar , super keyword is interpreted as Bar in Foo constructor and static methods and as Bar.
The TypeScript docs have a great example of constructor usage: class Greeter { greeting: string; constructor(message: string) { this. greeting = message; } greet() { return "Hello, " + this. greeting; } } let greeter = new Greeter("world");
Not supported by TypeScript. You can however exploit the fact that member functions are on prototype and you can call
anything with this
so SomeBaseClass.prototype.methodName.call(this,/*other args*/)
Example:
class Foo{
a(){alert('foo')}
}
class Bar extends Foo{
a(){alert('bar')}
}
class Bas extends Bar{
a(){Foo.prototype.a.call(this);}
}
var bas = new Bas();
bas.a(); // Invokes Foo.a indirectly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With