JavaScript Course
JavaScript
/
Advanced

Prototype Chain

Definition

The mechanism by which objects in JavaScript inherit features from one another. If a property isn't found on the object itself, JS searches its internal `[[Prototype]]` link, forming a chain.

Explain Like I'm New

Imagine asking your dad for $20. He doesn't have it, so he asks your grandpa. Grandpa doesn't have it, so he asks great-grandpa. The Prototype chain is just objects delegating property lookups up the family tree until they hit the ultimate ancestor (`Object.prototype`).

Real World Example

When you call `[].map()`, the array itself doesn't have a `.map` property. JS looks up the prototype chain to `Array.prototype`, finds the function, and executes it.

Common Use Cases

  • •Memory optimization (sharing methods across instances)
  • •Understanding Class inheritance under the hood

Interactive Example

const parent = { greet: () => 'Hello' };
const child = { name: 'Alice' };

// Manually setting the prototype (Not recommended for performance)
Object.setPrototypeOf(child, parent);

console.log(child.name);  // "Alice" (Found on child)
console.log(child.greet()); // "Hello" (Not on child, found on parent!)

// Checking ownership
console.log(child.hasOwnProperty('name')); // true
console.log(child.hasOwnProperty('greet')); // false

Interview Questions

basic

  • What happens if a property is never found in the prototype chain?

intermediate

  • What is the difference between `__proto__` and `prototype`?

advanced

  • How do you check if a property belongs to the object itself or its prototype?

Flash Cards

Question

What happens if it's never found?

Click to reveal answer
Answer

The chain ends at `Object.prototype`, whose prototype is `null`. If the engine reaches `null` and still hasn't found the property, it returns `undefined`.

Question

What is the difference between __proto__ and prototype?

Click to reveal answer
Answer

`__proto__` is the actual hidden link on an *instance* that points to its creator's prototype. `prototype` is a property exclusively found on *constructor functions*, used as the blueprint to build the `__proto__` of new instances.