Prototype Chain Lookup and Delegation
When an object lacks an own property, JavaScript walks its internal [[Prototype]] pointer chain until it finds the key or hits null. Methods keep this bound to the original receiver inside the new Execution Context, while V8 Inline Caches memorize stable chains.
[[Prototype]] Chain Lookup & IC Fast-Path
Trace how V8 resolves obj.method() across own properties, prototype hops, and null termination.
Engine inspects instance Hidden Class for key 'render'. Not found on own descriptor table.
widget.render();- target
- widget
- ownKeys
- [ "id", "label" ]
- found
- false
Under the Hood
When [[Get]] misses on an object's own property map, V8 walks its internal [[Prototype]] pointer chain until it finds a matching descriptor or reaches null. V8 Hidden Classes guard hot prototype lookups with validity cells, so mutating any ancestor prototype invalidates every downstream Inline Cache.
- Own Descriptor Check:
[[Get]]inspects the receiver's own property map first. - Chain Traversal: On a miss, the engine reads
[[GetPrototypeOf]]()and repeats the lookup up toObject.prototype. - Prototype Validity Cells: V8 Hidden Classes guard cached prototype lookups with validity cells; mutating any ancestor prototype invalidates every downstream IC.
- Shadowing Trap: Assigning
obj.x = 1creates an own property onobjrather than mutating a prototype data property.
const proto = { role: 'viewer' };
const user = Object.create(proto);
const dict = Object.create(null); // No Object.prototype
console.log(user.role); // 'viewer' (1 hop)
console.log(dict.toString); // undefined (safe from pollution)Problem Genealogy
- 1995 (
__proto__& Constructor.prototype): Delegation bolted onto Java-likenewsyntax without a standardObject.create. - 2015 (ES6
class&Reflect.getPrototypeOf): Standardized syntax over the same[[Prototype]]delegation model. - 2022 (
Object.hasOwn): Replaced the error-proneObject.prototype.hasOwnProperty.call(obj, key)boilerplate.
Production Pitfalls
- Runtime
Object.setPrototypeOf: Swapping prototypes on live objects destroys V8's prototype validity cells and forces slow dictionary lookups. - Prototype Pollution: Deep-merging untrusted JSON containing
"__proto__"keys mutatesObject.prototypeacross the entire realm.
- When not to use plain
{}as a map: Never index arbitrary user keys (like"constructor"or"toString") on{}; useMaporObject.create(null).
Connections & Logical Next Steps
Element upgrades swizzle the DOM node's [[Prototype]] to the registered HTMLElement subclass.
Proxy traps intercept [[Get]] and [[GetPrototypeOf]] before ordinary prototype traversal runs.
Structured clone copies own enumerable properties and discards custom [[Prototype]] chains.
Temporal types expose immutable value methods on dedicated prototypes without mutating receivers.
Method calls resolved on the prototype bind this to the original receiver in the new stack frame.
V8 Inline Caches bypass repeated prototype walks by validating prototype-chain Hidden Classes.