Sub topic 12
Inheritance Before Classes: Linking Constructor Prototypes
This chapter assumes you know how constructor functions and new work, what Object.create does, and how call sets this. Before ES2015 added class, developers built inheritance by hand from those pieces. You will still see this pattern in older libraries, and it shows what extends does for you.
The Two Links You Need
Inheritance between two constructors, Animal and Dog, needs two separate things:
- Instance data. Each dog needs the fields that
Animalsets up, such asname. You get them by runningAnimalagainst the new dog'sthis. - Shared methods. Dog instances must be able to find methods on
Animal.prototype. You get them by makingAnimal.prototypethe[[Prototype]]ofDog.prototype.
The Full Pattern
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function () {
return `${this.name} makes a sound`;
};
function Dog(name) {
Animal.call(this, name); // run the parent constructor on this instance
}
Dog.prototype = Object.create(Animal.prototype);
Object.defineProperty(Dog.prototype, 'constructor', {
value: Dog,
writable: true,
configurable: true,
enumerable: false
});
Dog.prototype.speak = function () {
return `${Animal.prototype.speak.call(this)}, woof`;
};
const rex = new Dog('Rex');
console.log(rex.speak()); // "Rex makes a sound, woof"
console.log(rex instanceof Animal); // true
console.log(rex.constructor === Dog); // trueEach step does one job:
Animal.call(this, name)copies the parent's per-instance setup onto the dog.Object.create(Animal.prototype)makes a new, emptyDog.prototypewhose prototype isAnimal.prototype.Object.definePropertyputs backconstructor, which was lost when the originalDog.prototypewas replaced. Making it non-enumerable matches the default property, so it does not appear infor...in.Animal.prototype.speak.call(this)calls the parent's version of an overridden method. This is the manual equivalent ofsuper.speak().
Common Mistakes
| Mistake | What goes wrong |
|---|---|
| `Dog.prototype = Animal.prototype` | Both constructors share one object, so adding `Dog.prototype.speak` also overwrites `Animal`'s method. |
| `Dog.prototype = new Animal()` | Runs `Animal` with no arguments, which leaves a `name: undefined` property on the prototype and runs any side effects in the constructor. |
| Forgetting `Animal.call(this, ...)` | Methods are inherited, but instances lack the parent's fields. |
| Not restoring `constructor` | `rex.constructor` resolves to `Animal`, which breaks code that uses it to create new instances. |
| Adding methods before replacing `Dog.prototype` | The methods are attached to the old object and lost. |
Exercise
A developer writes Cat.prototype = Object.create(Animal.prototype) but leaves out Animal.call(this, name) inside Cat. What do new Cat('Tom').speak() and new Cat('Tom') instanceof Animal return?
Show answer
speak() returns "undefined makes a sound", because the method is inherited but name was never set on the instance. The instanceof check returns true, because Animal.prototype is still on the chain.
Exercise
Why does Dog.prototype = Animal.prototype break Animal instances once Dog overrides speak?
Show answer
Both constructors point to the same object. Assigning Dog.prototype.speak replaces Animal.prototype.speak, so every animal now says "woof".