liveBook · Manning

Shadowing

As explained in JavaScript in Depth

The condition in which an object's own property takes precedence over an inherited property with the same name.

Shadowing occurs when an object gains an own data property whose name matches an inherited property. Property lookup stops at the nearer property, so the own value takes precedence while the prototype remains unchanged. section 5.3.1

An inherited label becomes local

With drawerPrototype.label set to "default", drawer initially reads that inherited value. After drawer.label = "tax documents", reads return the new own value, while drawerPrototype.label remains "default". This rule applies to data properties; an inherited setter instead runs with this bound to drawer and commonly creates drawer._label. section 5.3.1

const drawerPrototype = { label: 'default' };
const drawer = Object.create(drawerPrototype);
console.log(drawer.label);  #A
drawer.label = 'tax documents';
console.log(drawer.label);  #B
console.log(drawerPrototype.label);  #A

Trace the before-and-after lookup behavior with a prototype-backed drawer and observe that assignment shadows rather than mutates the prototype. section 5.3.1

Use these distinctions when diagnosing surprising property behavior:

  • Reads traverse the prototype chain; writes create an own property or invoke an inherited setter. section 5.3.1
  • Inherited names can affect authorization and configuration, so distinguish own properties from prototype-provided values. section 5.3.1

How JavaScript in Depth covers it

How deep it goes
Traces data properties, accessors, setters, and prototype traversal, while deferring broader prototype-pollution treatment.
Examples
Uses drawer objects, inherited labels, accessor-backed labels, authorization checks, and configuration defaults.
What it assumes
Assumes JavaScript objects and prototypes while teaching property lookup and assignment behavior through examples.
What you can do after
Diagnose unexpected property values by separating own data properties from inherited properties and setters.
How it connects
Connects shadowing with prototype chains, property descriptors, accessors, setters, and prototype pollution.

Also explained in

Compare how 5 books explain shadowing →

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.