Sub topic 7
Shadowing: Writes Go to the Object, Not the Prototype
This chapter assumes you already know how property lookup walks the prototype chain, how to create objects with Object.create, and how this refers to the receiver in inherited methods. In the earlier chapters, values only travelled up the chain when you read them. This chapter covers writes, which follow different rules.
Reads Climb the Chain, Writes Stay Put
When you read obj.x, JavaScript checks obj first and then each prototype in turn. When you assign with obj.x = value, JavaScript normally creates or updates an own property on obj itself and leaves the prototype alone. The new own property then shadows the inherited one. Shadowing means that the own property is found first, so the prototype's value is still there but no longer visible through this object.
const defaults = { color: 'blue', size: 'M' };
const shirt = Object.create(defaults);
console.log(shirt.color); // "blue" (inherited)
shirt.color = 'red'; // creates an own property
console.log(shirt.color); // "red"
console.log(defaults.color); // "blue" (unchanged)
console.log(Object.hasOwn(shirt, 'color')); // true
const other = Object.create(defaults);
console.log(other.color); // "blue"This asymmetry is what makes prototypes useful as defaults. Every object reads shared values from the prototype until it needs its own, and giving one object its own value has no effect on its siblings.
Removing a Shadow with delete
delete removes only own properties. After you delete the shadowing property, the inherited value shows through again. Deleting through the child never reaches the prototype.
const defaults = { color: 'blue' };
const shirt = Object.create(defaults);
shirt.color = 'red';
delete shirt.color;
console.log(shirt.color); // "blue" (inherited value visible again)
delete shirt.color; // no own property left; does nothing
console.log(defaults.color); // "blue"Compound Assignment Also Shadows
Operators such as ++, +=, and ||= read first and then write. The read can come from the prototype, but the write always lands on the object.
const counterProto = { hits: 0 };
const page = Object.create(counterProto);
page.hits++; // reads 0 from the prototype, writes 1 to page
page.hits += 10;
console.log(page.hits); // 11
console.log(counterProto.hits); // 0
console.log(Object.hasOwn(page, 'hits')); // trueThe Full Assignment Algorithm
The rule "writes go to the object" has two exceptions. Both apply only when the property is not already an own property of the target and JavaScript finds it on the prototype chain:
- If the inherited property is a data property with
writable: false, the assignment fails. In sloppy mode it fails silently. In strict mode, which includes ES modules and class bodies, it throws aTypeError. - If the inherited property is an accessor (it has a setter), the setter runs with
thisset to the object, and no own property named after the accessor is created. If the accessor has a getter but no setter, the assignment fails in the same way as in case 1.
Gotcha: Inherited Read-Only Properties
'use strict';
const config = {};
Object.defineProperty(config, 'version', {
value: 1,
writable: false,
enumerable: true
});
const child = Object.create(config);
try {
child.version = 2;
} catch (e) {
console.log(e instanceof TypeError); // true
}
console.log(child.version); // 1
// defineProperty creates an own property directly and skips the check
Object.defineProperty(child, 'version', {
value: 2,
writable: true,
enumerable: true,
configurable: true
});
console.log(child.version); // 2
console.log(config.version); // 1A non-writable property on the prototype stops plain assignment on every object that inherits from it. Object.defineProperty defines the property instead of assigning it, so the inherited read-only flag does not block it.
Gotcha: Inherited Setters
const base = {
_celsius: 0,
get celsius() { return this._celsius; },
set celsius(v) { this._celsius = v; }
};
const room = Object.create(base);
room.celsius = 21; // runs base's setter with this === room
console.log(Object.hasOwn(room, 'celsius')); // false
console.log(Object.hasOwn(room, '_celsius')); // true (the setter wrote it)
console.log(room.celsius); // 21
console.log(base.celsius); // 0The setter decides where the data goes. Here it writes this._celsius, which shadows base._celsius on room. The accessor celsius itself stays shared on the prototype.
Summary of Write Outcomes
| Situation when assigning `obj.key = v` | Result |
|---|---|
| `key` is an own writable property | Own value updated |
| `key` not found anywhere | New own property on `obj` |
| Inherited writable data property | New own property shadows it |
| Inherited non-writable data property | Fails; `TypeError` in strict mode |
| Inherited accessor with setter | Setter called with `this === obj` |
| Inherited accessor without setter | Fails; `TypeError` in strict mode |
| `Object.defineProperty(obj, key, ...)` | Own property defined; inherited flags ignored |
Shadowing Does Not Protect Mutations
Shadowing applies only to assignment to the property itself. obj.list.push(x) reads list, which may be inherited, and then changes that array in place. No assignment to obj.list happens, so no shadow is created, and the change is visible to every object that shares the prototype. The chapter "Pitfall: Shared Mutable State on Prototypes" covers this in detail. The rule to keep in mind is that only = and the compound operators create shadows. Method calls on an inherited value do not.
Shadowing Methods
Methods follow the same rules as other properties. Assigning a function to an instance overrides the prototype method for that instance only:
const greeter = {
greet() { return `Hello from ${this.name}`; }
};
const a = Object.create(greeter);
a.name = 'A';
const b = Object.create(greeter);
b.name = 'B';
b.greet = function () { return `Hey, ${this.name} here`; };
console.log(a.greet()); // "Hello from A"
console.log(b.greet()); // "Hey, B here"
console.log(greeter.greet.call(b)); // "Hello from B" (still reachable)Per-instance overrides like this work, but each one adds an own function property to the object. If many objects need the same override, put it on a shared prototype instead.
Exercise
Given const p = { n: 1 }; const o = Object.create(p); o.n += 1; delete o.n; o.n *= 5;, what are o.n and p.n at the end?
Show answer
o.n is 5 and p.n is 1. The += reads 1 from p and writes 2 as an own property on o. The delete removes that own property, so o.n reads 1 from p again. The *= then reads 1 and writes 5 as a new own property on o. None of these steps changes p.
Exercise
In an ES module, const proto = Object.freeze({ mode: 'dark' }); const o = Object.create(proto); o.mode = 'light'; throws. Why, and how can you give o its own mode?
Show answer
Object.freeze makes mode non-writable, and an inherited non-writable data property blocks assignment on every object that inherits it. ES modules run in strict mode, so the failed assignment throws a TypeError. Use Object.defineProperty(o, 'mode', { value: 'light', writable: true, enumerable: true, configurable: true }), or create o with the property already set: Object.create(proto, { mode: { value: 'light', writable: true, enumerable: true, configurable: true } }).
Exercise
An object inherits an accessor total with a setter that runs this._total = v. After obj.total = 10, does Object.hasOwn(obj, 'total') return true?
Show answer
No. The inherited setter runs with this === obj and creates an own _total property. No own total property is created, because the accessor stays on the prototype.