super
Calls the parent constructor or accesses parent-class methods.
Syntax
super(args)
super.method()
Examples
class Animal {
constructor(name) { this.name = name; }
speak() { return this.name + " makes a sound"; }
}
class Dog extends Animal {
speak() { return super.speak() + " (woof)"; }
}
console.log(new Dog("Rex").speak());
Output
Rex makes a sound (woof)
class Base {
constructor(x) { this.x = x; }
}
class Derived extends Base {
constructor(x, y) {
super(x);
this.y = y;
}
}
const d = new Derived(1, 2);
console.log(d.x, d.y);
Output
1 2
Notes
- In a derived constructor, `super(...)` must be called before using
`this`.
- `super.prop` accesses the prototype chain of the home object, not
`this`.
- Valid only inside class methods and object-literal method shorthand.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 42 |
| firefox | 45 |
| safari | 9 |
| edge | 13 |
| node | 4.0 |