Sub topic 14
Subclassing with extends and super
This chapter assumes you know how class syntax maps to constructor functions and prototypes. It also assumes you can follow how lookup walks the prototype chain and how this is bound when a method is called.
What extends sets up
class Dog extends Animal creates two prototype links:
Dog.prototypegetsAnimal.prototypeas its[[Prototype]], so instances inherit the parent's methods.DoggetsAnimalas its[[Prototype]]. That link lets static members be inherited, which the next chapter covers.
super gives a subclass access to its parent. You use it in two forms:
super(...)inside a constructor runs the parent constructor.super.method()inside a method calls the parent's version of that method.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound`;
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // must run before any use of this
this.breed = breed;
}
speak() {
return `${super.speak()}, woof`;
}
}
const rex = new Dog('Rex', 'terrier');
console.log(rex.speak()); // "Rex makes a sound, woof"
console.log(rex.breed); // "terrier"
console.log(Object.getPrototypeOf(Dog.prototype) === Animal.prototype); // true
console.log(Object.hasOwn(rex, 'name')); // trueThe parent constructor sets name directly on rex, so name is an own property. speak exists on both prototypes. Dog.prototype.speak shadows Animal.prototype.speak, and super.speak() reaches past the shadowing copy to the parent's method.
The rule: super() before this
In a derived class, the parent constructor creates the object, not new. Until super() returns, this does not exist yet, and touching it throws.
class Animal {
constructor(name) {
this.name = name;
}
}
class Broken extends Animal {
constructor(name) {
this.name = name; // this is not initialized yet
super(name);
}
}
try {
new Broken('x');
} catch (e) {
console.log(e.name); // "ReferenceError"
}If you leave out the constructor entirely, JavaScript supplies constructor(...args) { super(...args); }. A constructor you write yourself must call super() or return an object. If it does neither, it throws a ReferenceError when it finishes.
How super finds the parent
super.method() does not use this to locate the parent. Each class method stores the object it was defined on, called its home object. super looks up the prototype of that home object. The lookup target is fixed when the method is defined, but the call still passes the current this. That is why this.name inside Animal.prototype.speak refers to rex.
Common errors
| Mistake | Result |
|---|---|
| Using `this` before `super()` | ReferenceError |
| A constructor that never calls `super()` | ReferenceError when the constructor finishes |
| Forgetting `super.` and calling `speak()` | ReferenceError, because `speak` is not a variable |
| Calling `this.speak()` inside an override of `speak` | Infinite recursion |
Exercise
Add a Puppy class that extends Dog with no constructor and overrides speak to append ", yip" using super. What does new Puppy('Bit', 'pug').speak() return?
Show answer
"Bit makes a sound, woof, yip". The default constructor passes both arguments to Dog. Puppy's super.speak() calls Dog.prototype.speak, which calls Animal.prototype.speak, and this stays the Puppy instance throughout.
Exercise
Why does super.speak() inside Dog.prototype.speak reach Animal.prototype.speak even though this is rex?
Show answer
super resolves through the method's home object, Dog.prototype, and looks up its prototype, Animal.prototype. It does not walk from this. The call still receives rex as this.