The Prototype Chain
Discover JavaScript's hidden inheritance system. Learn how objects magically share methods by searching up an invisible chain of prototypes.
What is a Prototype?
In JavaScript, almost every object has a hidden internal property called [[Prototype]] (historically accessed via __proto__).
This property is simply a reference (a pointer) to another object. That other object is called its 'prototype', and it acts as a fallback or parent.
The Property Lookup Algorithm
When you try to read a property on an object (like myArray.push()), JavaScript first checks the object itself.
If it doesn't find the property there, it doesn't immediately give up. Instead, it follows the __proto__ link to the prototype object and looks there.
If it still doesn't find it, it checks *that* object's prototype, walking up the 'Prototype Chain' until it either finds the property or hits null.
Where do array methods come from?
When you create an array const arr = [], it is born with a __proto__ pointing to Array.prototype.
Array.prototype is a massive object that holds all the standard array methods: .push(), .map(), .filter(), etc.
Because of the prototype chain, your tiny empty array instantly gains access to all those methods without having to copy them into its own memory!
The Fallback Mechanism
Trace the path JavaScript takes when you call .toString() on a simple array, watching it walk up the invisible prototype chain.
The Fallback Mechanism
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- JavaScript uses Prototypal Inheritance, not classical class-based inheritance.
- Every object has a hidden
__proto__property pointing to a fallback object. - Property lookups walk up this chain until the property is found or
nullis reached. - Methods like
.push()and.map()live onArray.prototype, not on the individual arrays. - The chain usually terminates at
Object.prototype, which points tonull.
Done with this concept?
Mark it complete to track your progress. No login needed.