liveBook · Manning

Prototype chain

As explained in JavaScript in Depth

The linked sequence of prototype objects that JavaScript searches when resolving an object's inherited properties.

A missing property is not necessarily absent. JavaScript first checks an object, then follows its linked [[Prototype]] objects until it finds the property or reaches null. Reading can therefore produce inherited values, while writing usually creates an own property that shadows the inherited one. section 5.11 section 5.3.2

One method, two drawers

const drawerPrototype = { open() { this.isOpen = true; } }; const drawer1 = Object.create(drawerPrototype); const drawer2 = Object.create(drawerPrototype); drawer1.open(); drawer2.open(); Both objects resolve open through the same prototype. During the call, this remains the object that initiated the lookup, so one method operates on separate instance state. section 5.3 section 5.4.2

Use precise operations when reasoning about inherited properties:

  • Object.getPrototypeOf(obj) returns the immediate prototype. section 5.3.2
  • 'prop' in obj checks the object and its entire prototype chain. section 5.3.2
  • Object.hasOwn(obj, 'prop') restricts the check to the object itself; it does not confuse property existence with truthiness. section 5.3.2
  • Changing a shared prototype changes behavior for every inheriting object, making dynamic adaptation powerful but potentially hazardous. section 5.3 section 5.11
const drawerPrototype = {
  open() { console.log('Opening drawer'); }
};
const drawer1 = Object.create(drawerPrototype);
const drawer2 = Object.create(drawerPrototype);
drawer1.open();
drawer2.open();

Inspect a minimal shared-prototype relationship in which two objects resolve the same open method. section 5.3

const drawerPrototype = { open() {} };
const drawer = Object.create(drawerPrototype);
console.log(Object.getPrototypeOf(drawer) === drawerPrototype);  #A
console.log(Object.getPrototypeOf(drawerPrototype) ===
            Object.prototype);  #A
console.log(Object.getPrototypeOf(Object.prototype) === null);  #A

Trace the chain from an object through Object.prototype to null. section 5.3.2

How JavaScript in Depth covers it

How deep it goes
Traces lookup, shadowing, mutation, inspection, and this binding, while deferring broader runtime optimization details.
Examples
Uses shared drawer objects, Object.create, chain inspection, in, Object.hasOwn, and class accessors.
What it assumes
Assumes familiarity with JavaScript objects and properties while establishing prototype lookup and method sharing directly.
What you can do after
Diagnose inherited-property bugs, inspect chains, share methods, and recognize prototype mutation or shadowing.
How it connects
Connects prototype chains to property descriptors, this, classes, prototype pollution, and hidden classes.

Also explained in

Compare how 3 books explain prototype chain →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.