super
keyword is used to call methods or access properties of a parent class.
- The
super(...args)
expression is valid only in class constructors. - The
super.prop
andsuper[expr]
expressions are valid in any method definition in both classes and object literals.
class Foo {
constructor(public name: string){}
getNameSeperator() {
return '-';
}
}
class FooBar extends Foo {
constructor(public name: string, public index: number){
super(name);
}
getFullName(){
return this.name + super.getNameSeperator() + this.index;
}
}
let fb = new FooBar('foo', 1);
console.log(fb.name); // foo
console.log(fb.getFullName()); // foo-1