logo

Prototype Chain

How JavaScript uses prototypes for inheritance and property lookup.

Short explanation

Objects in JavaScript delegate property lookup to their prototype. The prototype chain is the linked list of objects checked for property resolution, enabling inheritance and shared behavior.

Syntax example

function Person(name) {
  this.name = name;
}
Person.prototype.greet = function () {
  return `Hi, ${this.name}`;
};
const p = new Person('Jane');
console.log(p.greet()); // 'Hi, Jane'

How JS handles it internally

Engines follow the prototype chain to find properties. Hidden classes and inline caching speed up property access by giving objects similar shapes the same hidden class for optimized access.

FAQ

Q: Should I use prototypes directly or classes?

A: Use classes for readability; they are syntactic sugar over the prototype mechanism.