Prototypes & Inheritance
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
- Functions & ScopeProgramming Fundamentals
Methods attached to a prototype are functions with their own scope, invoked in the context of whichever object is doing the lookup; you need functions-as-values already established before 'shared methods on a prototype' is meaningful.
- Mutability & ReferencesProgramming Fundamentals
A prototype link is a reference from one object to another; the same aliasing/reference model already covered explains exactly what 'delegating to a shared prototype' means at the memory level.
Unlocks
Used in
- class syntax in modern JavaScript, which compiles down to prototype-chain setup
- Array.prototype.map and every built-in method — all live on a shared prototype, not duplicated per array
- Object.create() for explicit, class-free prototypal inheritance
- Debugging 'method not found' errors by understanding exactly where the prototype chain terminates
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
- MDN — Object prototypesarticle
Mastery checklist
- I can explain what the prototype chain is and how property lookup traverses it
- I can implement inheritance using Object.create() without the class keyword
- I can explain that class syntax is sugar over the same prototype mechanism
- I can predict whether a property lookup will find a value, find it on the prototype, or return undefined