Course contentsShow
← JavaScript
Lesson 705 of 2,72317. Prototypes and InheritanceFree lesson

Shadowing Properties

How defining a property on an object hides a same-named property from its prototype.

Shadowing Properties

What You'll Learn

You'll learn how defining a property directly on an object "hides" (shadows) a same-named property from its prototype chain.

The Core Idea

When you create a property on an object that has the same name as a property in its prototype, the object's own property takes precedence. The prototype property doesn't disappear—it's just hidden from view for that specific object. This is called shadowing.

Think of it like wearing a name tag over your shirt: if your shirt already has your name printed on it, people see the name tag first. The name on the shirt is still there, just covered up.

How It Works

When JavaScript looks up a property:

  1. It checks the object itself first
  2. Only if not found there does it check the prototype chain

Once a property is found on the object, the search stops—even if the prototype has the same property name.

const parent = {
  color: 'blue',
  size: 'medium'
};

const child = Object.create(parent);
console.log(child.color); // 'blue' (from prototype)

// Now shadow the property
child.color = 'red';
console.log(child.color); // 'red' (own property shadows prototype)

// Prototype property still exists unchanged
console.log(Object.getPrototypeOf(child).color); // 'blue'

// Verify ownership
console.log(child.hasOwnProperty('color')); // true
console.log(child.hasOwnProperty('size')); // false

The color property on child now shadows the color in its prototype. The prototype's color remains 'blue', but child.color returns 'red'.

Key Takeaway: When an object has its own property with the same name as a prototype property, the own property shadows (hides) the prototype property during lookups, though the prototype property remains unchanged.