Skip to curriculum
Jump to the editor

9.4. Inheritance without class

Medium
Topics

This question is in

9. Prototypes and classes
20–30 min

class Dog extends Animal is shorthand. Underneath, it builds two constructor functions and links their prototype objects, so a dog finds Animal's methods when Dog does not have them. Older code, some libraries and many interview rounds still write that link by hand, and knowing each step explains what extends and super actually do.

Write inherit, Animal and Dog in animals.js without the class keyword: constructor functions, prototype objects and the functions Object gives you.

Signature

JAVASCRIPT
function inherit(Child, Parent) // returns Child
function Animal(name)
function Dog(name, breed)
  • Animal: own property name. Methods speak() returns "<name> makes a sound."; eat(food) returns "<name> eats <food>.". A static function Animal.create(...args) builds a new instance of the constructor it is called on.
  • Dog: an Animal with the own properties name and breed. speak() returns Animal's sentence followed by " Woof!"; describe() returns "<name> is a <breed>.".
  • inherit(Child, Parent): links the two constructors and returns Child.

Requirements

  1. Animal and Dog are plain functions, not classes.
  2. An animal's only own key is name; speak and eat live once on Animal.prototype.
  3. A dog's own keys are name and breed, in that order, and each dog keeps its own values.
  4. A dog uses Animal.prototype.eat itself (not a copy): Object.getPrototypeOf(Dog.prototype) is Animal.prototype, and a dog is an instance of Dog, Animal and Object.
  5. Dog.prototype.speak overrides Animal's and builds on it: "Rex makes a sound. Woof!".
  6. Plain animals get none of Dog's methods, and still say "… makes a sound.".
  7. Dog.prototype.constructor is Dog and is not enumerable; Animal.prototype.constructor is still Animal.
  8. A change made to Animal.prototype later reaches dogs that already exist.
  9. Dog inherits Animal's static functions: Object.getPrototypeOf(Dog) is Animal, and Dog.create("Rex", "beagle") builds a Dog.
  10. Animal(…) and Dog(…) called without new throw a TypeError.
  11. inherit(Child, Parent) works for any two constructor functions: it does all of the linking in requirements 4, 7 and 9, returns Child, and never calls Parent. Methods added to Child.prototype afterwards stay off Parent.prototype.

Examples

JAVASCRIPT
const rex = new Dog("Rex", "beagle");
rex.speak();        // "Rex makes a sound. Woof!"
rex.eat("bones");   // "Rex eats bones."
rex.describe();     // "Rex is a beagle."
Object.keys(rex);   // ["name", "breed"]
JAVASCRIPT
new Animal("Tom").speak();      // "Tom makes a sound."
new Animal("Tom").describe;     // undefined
Dog.create("Fido", "pug") instanceof Dog; // true
Dog("Rex");                     // TypeError
JAVASCRIPT
function Shape(sides) { this.sides = sides; }
function Square() { Shape.call(this, 4); }
inherit(Square, Shape);         // returns Square, and Shape did not run
new Square() instanceof Shape;  // true

Environment

  • Plain JavaScript in a browser page; the tests call the globals inherit, Animal and Dog from animals.js.
  • The class keyword is not allowed (the tests check that Animal and Dog are not classes). Object.create, Object.setPrototypeOf, Object.defineProperty and call are fine.

Hints

  1. Child.prototype = new Parent() makes the link, but what else does it do that inherit must not?
  2. inherit replaces Dog.prototype with a new object. What happens to methods you put on the old one before calling it?
  3. Inside Animal.call(this, name), is new.target set?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…