Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript super.super call

Tags:

typescript

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.

like image 520
István Avatar asked Sep 16 '14 09:09

István


People also ask

What is super call in TypeScript?

The super keyword is used to call the constructor of its parent class to access the parent's properties and methods.

How do you call a super class constructor in TypeScript?

You can call the base class constructor from the child class by using the super() which will execute the constructor of the base class.

What is super () in angular?

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.

How do you define a constructor in TypeScript?

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");


1 Answers

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
like image 195
basarat Avatar answered Nov 18 '22 05:11

basarat