Prototype Chain
The prototype chain is the sequence of objects the engine walks when looking up a property: the object itself, then its prototype, then that object's prototype, until it reaches null.
Detailed Explanation
Prototypes link objects into a chain. Property lookup follows that chain, one step at a time, and stops at the very first object that has the property. If no object in the chain has it, the result is undefined.
Take a concrete chain. You have an AdminUser that extends User. An instance's chain is: the instance, then AdminUser.prototype, then User.prototype, then Object.prototype, then null. Calling admin.hasOwnProperty("email") walks four objects before it finds the method on Object.prototype. Calling admin.email finds it immediately on the instance. The chain always terminates, because Object.prototype's own prototype is null.
Shadowing follows from "first hit wins". If AdminUser.prototype defines describe, it hides User.prototype.describe for admins. The parent version still exists and is still reachable through super.describe(), but plain lookup never gets that far.
Here is the part most explanations skip. Reading walks the chain; writing mostly does not. obj.count = 1 does not modify the prototype. It creates an own property on obj that shadows whatever was inherited. That is why one instance incrementing an inherited counter never affects its siblings, and also why a method inherited from a prototype can be overridden on a single instance.
Assignment has three exceptions, and they matter:
- If the chain holds an inherited setter for that name, the setter runs with
thisbound to the original object. No own property is created. - If the chain holds an inherited non-writable data property, the assignment fails: silently in sloppy mode, with a
TypeErrorin strict mode. Object.definePropertynever consults the chain at all. It always defines an own property, which is also why class fields (defined, not assigned) can shadow an inherited setter instead of triggering it.
Two operators reflect the distinction. in searches the whole chain, so "toString" in {} is true. Object.hasOwn(obj, key), the ES2022 replacement for obj.hasOwnProperty(key), checks only own properties. for...in enumerates enumerable properties along the chain, while Object.keys returns own enumerable string keys only. instanceof is a chain question too: it asks whether Constructor.prototype appears anywhere in the object's chain.
This design has a security consequence with a name: prototype pollution. If attacker-controlled input can write to Object.prototype, usually through a key such as __proto__ or constructor.prototype in a recursive merge or JSON-driven configuration, then every object in the program suddenly inherits that value. A check like if (user.isAdmin) can become true for objects that never had the property. Defences are to reject those keys, use Object.create(null) or Map for data-driven lookups, and Object.freeze(Object.prototype) in hardened environments.
The cost of chains is depth. Each miss is another hop, and although engines cache lookups by object shape, a chain ten levels deep is measurably slower than two, and megamorphic call sites lose their caching entirely. Deep hierarchies are also harder to read: finding where a method is actually defined means walking the chain by hand. Prefer shallow chains and composition.
Code Examples
class User {
constructor(email) {
this.email = email;
}
describe() {
return `user ${this.email}`;
}
}
class AdminUser extends User {
describe() {
return `admin ${this.email}`; // shadows User.prototype.describe
}
}
const admin = new AdminUser("ada@example.com");
// Walk the chain by hand.
let node = admin;
const chain = [];
while (node !== null) {
chain.push(node.constructor?.name ?? "null-prototype");
node = Object.getPrototypeOf(node);
}
console.log(chain); // => [ 'AdminUser', 'AdminUser', 'User', 'Object' ]
// (the first entry is the instance, the rest are prototype objects)
console.log(admin.describe()); // => admin ada@example.com
console.log(Object.hasOwn(admin, "describe")); // => false
console.log("describe" in admin); // => true (found up the chain)
console.log(admin instanceof User); // => true (User.prototype is in the chain)
console.log(Object.getPrototypeOf(Object.prototype)); // => null (chain ends)"use strict";
const base = { count: 0 };
const child = Object.create(base);
// Reading finds it on the prototype.
console.log(child.count); // => 0
// Writing does NOT touch the prototype; it creates an own property.
child.count = 5;
console.log(child.count, base.count); // => 5 0
console.log(Object.hasOwn(child, "count")); // => true
// Exception 1: an inherited setter intercepts the assignment.
const audited = {
set label(value) {
this._label = value.trim(); // `this` is the receiver, not `audited`
},
};
const entry = Object.create(audited);
entry.label = " draft ";
console.log(entry._label); // => draft
console.log(Object.hasOwn(entry, "label")); // => false (no own `label`)
// Exception 2: an inherited non-writable property blocks the write.
const locked = Object.defineProperty({}, "id", { value: 1, writable: false });
const attempt = Object.create(locked);
try {
attempt.id = 2;
} catch (err) {
console.log(err.constructor.name); // => TypeError (silent without strict mode)
}Common Gotchas
Assignment does not walk the chain the way reading does
`child.total = 1` creates an own property and shadows the inherited one. Code that expects to be updating shared state on the prototype will find every instance quietly holding its own copy instead.
Prototype pollution is a real vulnerability
a recursive merge that accepts a `__proto__` or `constructor` key from user input can write onto `Object.prototype` and change the behaviour of every object in the process. Reject those keys explicitly, and use `Map` or `Object.create(null)` for lookups keyed by untrusted data.
`for...in` includes inherited enumerable properties
it will pick up properties added to prototypes by older libraries. Use `Object.keys`, `Object.entries`, or `for...of` over `Object.entries` when you mean own properties only.
`instanceof` is a chain check, not a type check
it returns `true` whenever `Constructor.prototype` appears anywhere in the chain, so it fails across realms such as an iframe or a Node worker where each realm has its own `Array.prototype`. `Array.isArray` and `Symbol.hasInstance` exist for those cases.