Understanding the Prototype Chain
A prototype is an ordinary object, so it can have a prototype of its own. The links form the prototype chain. When you read a property, JavaScript checks the object first, then its prototype, then that object’s prototype, and so on, and it stops at the first match. If it reaches the end of the chain, which is null, the result is undefined.
Prototypes introduces the single link; this page follows the whole chain.
Looking a property up
Section titled “Looking a property up”Here bread inherits from food, which inherits from item. Each lookup stops at the nearest object that has the property:
const item = { vat: 0.2, label() { return `£${this.price} plus ${this.vat * 100}% VAT`; },};const food = Object.create(item);food.vat = 0;const bread = Object.create(food);bread.price = 1.5;
console.log(bread.price);console.log(bread.vat);console.log(bread.label());console.log(bread.colour);This prints:
1.50£1.5 plus 0% VATundefinedprice is found on bread itself. vat is found on food, so the 0.2 on item is never reached. label() is found on item, but this is still bread, so it reads bread.price and the vat from food. Nothing in the chain has colour, so the result is undefined rather than an error. Calling a missing method is different, because undefined is not a function and so it throws a TypeError.
Walking the chain
Section titled “Walking the chain”Object.getPrototypeOf() steps one link at a time, and isPrototypeOf() checks whether an object appears anywhere in another object’s chain:
const item = { vat: 0.2 };const food = Object.create(item);const bread = Object.create(food);
console.log(Object.getPrototypeOf(bread) === food);console.log(Object.getPrototypeOf(food) === item);console.log(Object.getPrototypeOf(item) === Object.prototype);console.log(Object.getPrototypeOf(Object.prototype));console.log(item.isPrototypeOf(bread));This prints:
truetruetruenulltrueSo the chain is bread → food → item → Object.prototype → null.
You will also see obj.__proto__ used to read or set the prototype. It is a legacy accessor kept for compatibility, and it does not exist on objects created with Object.create(null), so use Object.getPrototypeOf() and Object.setPrototypeOf() instead.
Constructor functions and their prototype property
Section titled “Constructor functions and their prototype property”Every ordinary function has a prototype property. When you call a function with new, the new object’s prototype is set to that function’s prototype object, so methods placed there are shared by every instance:
function Account(owner, balance) { this.owner = owner; this.balance = balance;}
Account.prototype.deposit = function (amount) { this.balance += amount; return this.balance;};
const acc = new Account('Lee', 100);console.log(acc.deposit(50));console.log(Object.getPrototypeOf(acc) === Account.prototype);console.log(Object.getPrototypeOf(Account.prototype) === Object.prototype);console.log(Object.hasOwn(acc, 'deposit'));console.log(acc.prototype);This prints:
150truetruefalseundefinedThe chain is acc → Account.prototype → Object.prototype → null. The names are easy to confuse: Account.prototype is the object that instances inherit from, not the prototype of Account itself, and an instance such as acc has no prototype property at all. Methods are best placed on the prototype object rather than assigned to this in the constructor, because a method assigned to this is created again for every instance. Using the new( ) Operator on Constructor Functions explains what new does.
Writing does not walk the chain
Section titled “Writing does not walk the chain”Reading a property searches the chain, but assigning one does not. Assignment creates or updates an own property on the object, which shadows the inherited one and leaves the prototype unchanged. Deleting the own property uncovers the inherited value again:
const settings = { theme: 'light', fontSize: 14 };const mine = Object.create(settings);
mine.theme = 'dark';console.log(mine.theme, settings.theme);
delete mine.theme;console.log(mine.theme);This prints:
dark lightlightThe exception is a property defined with a setter or marked read-only somewhere up the chain, where the setter runs or the assignment fails instead.
Steve Griffith covers this topic in his video Understanding the JavaScript Prototype Chain.