Programming languages / Prototypal inheritance

Prototypal inheritance

JavaScript objects inherit by pointing at other objects. Seven steps follow one property lookup up the prototype chain, then show what the class keyword really builds.

Intermediate7 steps~7 min
Object — a bag of its own properties
Prototype link — hidden pointer to the next object up
Lookup — asks each object in turn: yes or no
// THE SHORT VERSION

In 30 seconds

Almost every JavaScript object carries a hidden link to another object, its prototype.

When you read a property the object does not have, the engine follows that link and looks there, then follows the next link, until it finds the property or reaches null. That path is the prototype chain.

Nothing is copied: objects share behaviour by delegating to the objects they link to. The class keyword is a tidier way to set up these same links, not a different system.

Prototypal inheritance, step by step

STEPS01 / 07

Simplified: assignment does climb far enough to notice an inherited setter or read-only property, which changes the result. Classes also add features of their own, such as private fields.

STEP 01 / 07 One object links to another
THE MATH
rabbit = Object.create(animal) prototype of rabbit: animal
  1. Step 1: One object links to another. Every JavaScript object has one hidden link, usually to another object, called its prototype. Object.create(animal) makes a new empty object whose link leads to animal. Then rabbit gets a property of its own, jumps. They stay two separate objects, joined by that link.
  2. Step 2: Own property: found at once. To read rabbit.jumps the engine first looks at the properties stored on rabbit itself. jumps is there, so the search stops immediately and the answer is true. The link to animal is not used at all.
  3. Step 3: Not here? Climb the link. rabbit has no property named eats. Instead of giving up, the engine follows the hidden link to animal and asks the same question there. animal has eats, so rabbit.eats is true. rabbit acts as if it had the property without holding a copy. This is inheritance by delegation.
  4. Step 4: The chain ends at null. animal has a prototype too: Object.prototype, the built-in object that supplies toString and other basics. Its own link is null, which marks the end. Looking up rabbit.flies visits all three objects, finds nothing, reaches null and gives undefined. Reading a missing property is not an error.
  5. Step 5: Writing never climbs. Assignment works differently from reading. rabbit.eats = false does not follow the link; it creates a new property on rabbit itself. That own property now hides the inherited one, which is called shadowing. animal is untouched, and so is every other object that links to it.
  6. Step 6: What class really builds. The class keyword builds the same structure. Methods written in a class body are stored on an object called Rabbit.prototype. new Rabbit() creates r with its link set to that object, and extends links Rabbit.prototype to Animal.prototype. Calling r.eat() is the same climb as before.
  7. Step 7: Blueprints vs live links. In classical languages such as Java or C++, a class is a blueprint fixed before the program runs, and objects are built from it. In JavaScript there are only objects linked to objects, and lookups happen while the program runs. Add nap to Rabbit.prototype and rabbits that already exist can use it at once.
VALUES SO FAR
rabbitlinks to animalprototype
jumps····own
eats····inherited
flies····missing
eats····shadow
r.eat()····class
r.nap()····live

Spotted a mistake on this page?