Chapter XIX · Prototypes
Prototype Chain
The prototype chain is the lookup mechanism where JavaScript searches up through linked object prototypes to resolve property and method calls.
01 How It Works
Think of checking successive family ancestry lineage levels to find a shared trait or asset.
If a property is missing on an object, the engine checks its `__proto__`, then its prototype's prototype, continuing until reaching `null` at the top of the chain.
let arr = [1, 2, 3];
console.log(arr.hasOwnProperty('length')); // Found up the prototype chain
1. Request property evaluation on a target object.
2. Check local object properties first.
3. Traverse upward through the prototype chain until found or `null` is reached.
02 Practical Example
Here is how you might see this concept applied in real-world code:
const child = { name: "Child" };
const parent = { lastName: "Doe" };
Object.setPrototypeOf(child, parent);
console.log(child.lastName); // "Doe" (traversed via prototype chain)
Key Takeaway
The prototype chain allows objects to inherit features hierarchically from parent prototypes.