super
Calls the parent class's constructor or method.
Available since: ES2015
Syntax
super(args) / super.method(args)Examples
The parent's constructor and method:
class Shape {
constructor(name) {
this.name = name;
}
describe() {
return `a ${this.name}`;
}
}
class Circle extends Shape {
constructor(radius) {
super('circle');
this.radius = radius;
}
describe() {
return `${super.describe()} with radius ${this.radius}`;
}
}
console.log(new Circle(2).describe());Output:
a circle with radius 2Notes
In a subclass constructor you cannot use this before calling super() — it throws a ReferenceError.
See also
In the tutorial
- Inheritance — Class inheritance in JavaScript — extends, super, overriding methods and instanceof.