`super`
super is syntax for talking to the parent class. super(...) runs the parent constructor. super.method(...) calls the parent's version of a method.
Detailed Explanation
super exists only in class constructors, class methods, and object-literal methods that use the concise method syntax. It is not a value. You cannot const parent = super or pass super into a helper. The engine resolves it from the method's home object — the prototype or object where the method was defined — not from this.
In a subclass constructor, super(args) is required before this is used, unless you return an object (rare). It calls the parent constructor with the given arguments and binds this to the new instance. Field initialisers in the subclass run immediately after super returns. That order matters: a subclass field cannot read this.x if x is also a subclass field that has not run yet, but it can read properties the parent constructor set.
In a method, super.describe() looks up describe on the parent prototype and calls it with the current this. That last part is the useful one: you override a method, then reuse the parent body without copying it. super.describe is the function; super.describe() is the call. Using super in an arrow defined in a class field works, because the arrow is created in a context that knows the class home. Using super in a regular function nested inside a method does not, unless that nested function is itself a method.
Object literals can use super too:
const child = {
__proto__: parent,
speak() {
return super.speak() + "!";
},
};
The method must be written as speak() {}, not speak: function () {}. The latter has no [[HomeObject]].
super in a static method talks to the parent constructor's static side: super.idFrom(email) inside static idFrom calls User.idFrom.
What it costs you: a hard error when you forget it, and a subtle bug when you think super follows this. If you copy a method onto another prototype with Admin.prototype.describe = User.prototype.describe, super inside that method still talks to User's parent, because the home object was User.prototype when the method was defined. Reassigning methods breaks super.
Reach for super(...) in every subclass constructor you write. Reach for super.method() when you are extending behaviour, not replacing it. Prefer composition if you find yourself calling super through three levels.
Code Examples
class Account {
constructor(id) {
this.id = id;
this.balance = 0;
}
label() {
return `account ${this.id}`;
}
}
class Savings extends Account {
constructor(id, rate) {
super(id);
this.rate = rate;
}
label() {
return `${super.label()} @ ${this.rate}`;
}
}
const s = new Savings("s-1", 0.03);
console.log(s.label()); // => account s-1 @ 0.03
console.log(s.balance); // => 0class User {
static prefix() {
return "user";
}
}
class Admin extends User {
static prefix() {
return `${super.prefix()}-admin`;
}
}
console.log(Admin.prefix()); // => user-adminclass Broken extends Account {
constructor(id) {
try {
console.log(this);
} catch (err) {
console.log(err.name); // => ReferenceError
}
super(id);
}
}
new Broken("x");Common Gotchas
`this` before `super()` throws
even `this` in a default argument of the subclass constructor is too early.
`super` is tied to the method's home object
extracting or copying the function can make `super` resolve to the wrong parent, or throw.
Object-literal `super` needs method shorthand
`speak: function () { super.speak(); }` is a syntax error.
Default subclass constructors call `super(...args)`
if the parent constructor changes arity, subclasses without their own constructor follow that change.