Prototypes & OOPcritical

Prototype Chain

Every JavaScript object has an internal [[Prototype]] link to another object (or null). Property lookups traverse this chain: if a property isn't found on the object itself, the engine checks [[Prototype]], then [[Prototype]]'s [[Prototype]], and so on.

Memory anchor

Prototype chain = a family tree of sticky notes. You ask the kid for a recipe; if they don't have it, ask mom, then grandma, all the way up. Everyone shares grandma's recipes without making copies.

Expected depth

Object.getPrototypeOf(obj) reads [[Prototype]]; Object.setPrototypeOf() sets it (avoid — performance killer). The prototype chain ends at Object.prototype (whose [[Prototype]] is null). hasOwnProperty() checks only the object itself, not the chain. The prototype of functions is Function.prototype; the prototype of Function.prototype is Object.prototype. Array.prototype methods (map, filter) are on the Array.prototype object, not on individual arrays.

Deep — senior internals

V8 uses hidden classes (called 'Maps' internally) to represent the shape of objects — a flat lookup table for own properties with an inline cache for fast access. When a property is found on the prototype chain, V8 caches the lookup result in an inline cache at the call site (polymorphic inline cache, PIC). If the prototype chain shape changes (via Object.setPrototypeOf or property addition), V8 must deoptimize and re-walk the chain. This is why mutating [[Prototype]] at runtime is catastrophically slow — always establish the chain at creation time via new, Object.create, or class.

🎤Interview-ready answer

The prototype chain is JavaScript's delegation mechanism for property lookup. When you access a property, the engine checks the object itself, then walks [[Prototype]] links until it finds the property or hits null. Classes use this same mechanism — methods live on the prototype, not on each instance, which saves memory. hasOwnProperty() lets you distinguish own properties from inherited ones.

Common trap

for...in iterates over ALL enumerable properties including inherited ones from the prototype chain. Use Object.keys() for own enumerable properties, or add a hasOwnProperty check inside for...in. This is the source of many subtle bugs when augmenting built-in prototypes like Array.prototype.

Related concepts