Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

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 2

Notes

In a subclass constructor you cannot use this before calling super() — it throws a ReferenceError.

See also

  • extends — Creates a subclass that inherits the parent's properties and methods.
  • class — Declares a class — a template for objects with shared methods.

In the tutorial

  • Inheritance — Class inheritance in JavaScript — extends, super, overriding methods and instanceof.

Thanks for reading!

Donate to the site — we work hard for you.