Sub topic 12
Calling Parent Constructors and Methods with super
This chapter assumes you already know how to declare a subclass with extends and how methods live on the prototype. The super keyword lets a subclass reach its parent class in two ways. You call super(...) to run the parent constructor, and you use super.method() to call a parent method.
super() in the Constructor
A derived class is any class declared with extends. When new runs on a derived class, the derived constructor does not create the object itself. The parent constructor creates it. Until super() runs, this does not exist yet.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound.`;
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // Animal's constructor creates and initializes this
this.breed = breed; // safe: this now exists
}
speak() {
return `${super.speak()} Woof!`;
}
}
const d = new Dog('Rex', 'Beagle');
console.log(d.speak()); // 'Rex makes a sound. Woof!'
console.log(d.name, d.breed); // 'Rex' 'Beagle'If you leave out the constructor entirely, JavaScript supplies a default one, constructor(...args) { super(...args); }. It forwards every argument to the parent unchanged.
Common Errors with super()
| Mistake | Result |
|---|---|
| Using `this` before `super()` | `ReferenceError` |
| Writing a derived constructor that never calls `super()` | `ReferenceError` when the constructor returns |
| Calling `super()` twice | `ReferenceError`, because `this` is already initialized |
| Calling `super()` in a class without `extends` | `SyntaxError` |
class Cat extends Animal {
constructor(name) {
this.indoor = true; // ReferenceError: must call super first
super(name);
}
}
try {
new Cat('Tom');
} catch (err) {
console.log(err instanceof ReferenceError); // true
}super.method() for Overrides
When a subclass defines a method with the same name as a parent method, the subclass version shadows the parent's version. Writing this.speak() inside Dog.prototype.speak would call itself and recurse forever. super.speak() starts the lookup at the parent's prototype instead, and it still uses the current this. That is why this.name resolves to 'Rex' in the earlier example.
You can call super.method() from any method, getter, or setter, not only from the override with the same name. Inside static methods, super refers to the parent class itself, so super.create() calls the parent's static create.
class Base {
static create() {
return new this();
}
}
class Derived extends Base {
static create() {
const obj = super.create(); // Base.create with this === Derived
obj.tag = 'derived';
return obj;
}
}
const o = Derived.create();
console.log(o instanceof Derived, o.tag); // true 'derived'super is bound to the class where the method was defined, not to the object it is called on. Copying a method to another object does not change what its super refers to.
Exercise
A subclass constructor reads constructor(id) { this.id = id; super(); }. What happens when you call new on it, and how do you fix it?
Show answer
It throws a ReferenceError, because this is not initialized until super() runs. Move super(); to the first line, before this.id = id;.
Exercise
Inside an override toString() { ... }, why should you write super.toString() and not this.toString() to get the parent's result?
Show answer
this.toString() looks up the method starting at the instance, finds the override itself, and recurses until the stack overflows. super.toString() starts the lookup at the parent prototype and still passes the current instance as this.