Every object in JavaScript has a hidden link to another object called its prototype. When you try to access a property or method that doesn't exist directly on an object, JavaScript doesn't just give up — it looks at the object's prototype, then that prototype's prototype, and so on, until it finds the property or runs out of links. This chain is called the prototype chain, and it's the real mechanism behind inheritance in JavaScript.
This matters because it explains why things work, not just that they work — including why the newer class syntax behaves the way it does.
Seeing the Prototype Chain in Action
const animal = {
eats: true,
walk() {
console.log("Animal walks");
},
};
const rabbit = {
jumps: true,
};
// Link rabbit's prototype to animal
Object.setPrototypeOf(rabbit, animal);
console.log(rabbit.jumps); // own property
console.log(rabbit.eats); // found via the prototype chain
rabbit.walk(); // method found via the prototype chain
Output:
true
true
Animal walks
rabbit doesn't actually have an eats property or a walk method of its own — JavaScript follows the link to animal and finds them there.
Object.create() — The Cleaner Way to Set a Prototype
Object.setPrototypeOf works but is considered slow and is rarely used in real code. The more common approach is Object.create(), which creates a brand-new object with a given prototype right from the start:
const rabbit2 = Object.create(animal);
rabbit2.jumps = true;
console.log(rabbit2.eats); // inherited from animal
console.log(Object.getPrototypeOf(rabbit2) === animal); // true
Output:
true
true
How This Connects to class
When you write a JavaScript class, the engine is quietly setting up this exact same prototype chain for you. Every method you define inside a class body is actually attached to that class's prototype object, shared by every instance — which is also why calling a method doesn't make a separate copy of that function for each object. extends works by linking one class's prototype to another's, which is the real mechanism behind inheritance with extends and super.
Understanding prototypes isn't something you'll need to write out by hand every day — but it demystifies a lot of "magic" behavior in JavaScript, and it's a common interview topic precisely because it separates people who memorized class syntax from people who understand what it's doing underneath.
