Sub topic 11
Creating Subclasses with extends
This chapter assumes you can write a class with a constructor and instance methods, and that you know methods live on the class's prototype object. Here you will use extends to build one class on top of another.
The Basic Pattern
A subclass is a class that inherits the methods of another class, called its parent or base class. You declare one with extends:
class Vehicle {
constructor(wheels) {
this.wheels = wheels;
}
describe() {
return `A vehicle with ${this.wheels} wheels.`;
}
}
class Truck extends Vehicle {
load(tons) {
return `Loading ${tons} tons onto ${this.wheels} wheels.`;
}
}
const t = new Truck(6);
console.log(t.describe()); // 'A vehicle with 6 wheels.'
console.log(t.load(10)); // 'Loading 10 tons onto 6 wheels.'Truck has no constructor of its own. When a subclass leaves out its constructor, JavaScript supplies a default one that passes every argument on to the parent: constructor(...args) { super(...args); }. That is how new Truck(6) ends up setting this.wheels.
What extends Wires Up
extends links two prototype chains. The first link lets instances find parent methods. The second lets the subclass inherit the parent's static members.
class Vehicle {
static create() {
return new this(4);
}
constructor(wheels) {
this.wheels = wheels;
}
}
class Truck extends Vehicle {}
console.log(Object.getPrototypeOf(Truck.prototype) === Vehicle.prototype); // true
console.log(Object.getPrototypeOf(Truck) === Vehicle); // true
console.log(Truck.create() instanceof Truck); // trueWhen you call t.describe(), the engine checks t itself first, then Truck.prototype, then Vehicle.prototype, and stops at the first match.
Writing Your Own Subclass Constructor
If the subclass needs extra setup, it can define its own constructor. It must call super(...) before it touches this, because the parent constructor is the code that creates the object. The next chapter covers super in full.
class Vehicle {
constructor(wheels) {
this.wheels = wheels;
}
}
class Truck extends Vehicle {
constructor(wheels, capacity) {
super(wheels);
this.capacity = capacity;
}
}
console.log(new Truck(6, 20)); // Truck { wheels: 6, capacity: 20 }Common Errors
| Mistake | Result |
|---|---|
| Using `this` before `super()` in a subclass constructor | `ReferenceError: Must call super constructor in derived class before accessing 'this'` |
| Defining a constructor that never calls `super()` and returns nothing | `ReferenceError` when `new` finishes |
| Extending something that is not a constructor, such as a plain object | `TypeError: Class extends value ... is not a constructor or null` |
| Assuming a method defined on the subclass alters the parent | The parent is unchanged; only subclass instances see it |
Exercise
Given class A { constructor(x) { this.x = x; } } and class B extends A { constructor(x) { this.y = 1; super(x); } }, what happens when you run new B(5), and how do you fix it?
Show answer
It throws a ReferenceError because this.y = 1 runs before super(x), and the object does not exist yet. Move super(x) to the first line of B's constructor, then assign this.y = 1.
Exercise
If class C extends A {} declares no constructor, what does new C(7).x return, and why?
Show answer
It returns 7. C gets a default constructor that forwards all arguments to super, so A's constructor sets this.x = 7.