JavaScript Runtime7h estimated

Prototypes & Inheritance

Difficulty
Importance

The problem

Objects often need to share behavior (methods) without duplicating that behavior into every single instance — prototypal inheritance lets many objects delegate to a shared object for anything they don't have themselves, saving memory and centralizing behavior.

Why now

Mutability and references already explained that objects are accessed by reference, not copied; prototypal inheritance extends that idea one step further — an object can reference another object (its prototype) specifically to borrow its properties and methods when its own lookup comes up empty.

Mental model

Every object has a hidden link to another object, its prototype. Looking up a property that doesn't exist directly on an object doesn't fail immediately — it walks up the prototype chain, checking each linked object in turn, until it finds the property or runs out of chain. class syntax is just a more familiar-looking wrapper over this exact same prototype-chain mechanism.

Requires

Unlocks

Used in

Projects

  • Implement simple inheritance using Object.create() and raw prototypes, without using the class keyword, then rewrite it with class and compare
  • Walk the prototype chain of a built-in array instance using Object.getPrototypeOf() repeatedly until reaching null

Examples

  • [1,2,3].map(...) works because every array instance's prototype chain includes Array.prototype, which defines map once for all arrays
  • class Dog extends Animal {} sets up Dog.prototype's prototype to be Animal.prototype — the same chain mechanism, friendlier syntax

Resources

Mastery checklist