Sub topic 6
Shadowing: Writes Go to the Object, Not the Prototype
This chapter assumes you know how property lookup walks the prototype chain and how to create an object with Object.create. Reading a property searches up the chain. Writing a property usually does not.
Assignment creates an own property
When you assign obj.key = value, JavaScript normally creates or updates a property on obj itself. The prototype is left unchanged. The new own property shadows the inherited one, which means lookups find it first and stop there.
const defaults = { color: 'blue', size: 'M' };
const shirt = Object.create(defaults);
console.log(shirt.color); // "blue" (inherited)
shirt.color = 'red'; // creates an own property on shirt
console.log(shirt.color); // "red"
console.log(defaults.color); // "blue" (prototype unchanged)
console.log(Object.hasOwn(shirt, 'color')); // true
delete shirt.color; // removes only the own property
console.log(shirt.color); // "blue" (inherited value shows again)delete removes only the object's own property, so the inherited value becomes visible again. Deleting through the child never removes anything from the prototype.
When assignment does not shadow
Two kinds of inherited property change this default.
- An inherited setter. If the chain has an accessor with a
setfunction for that key, assignment calls the setter withthisset to your object. No own property is created unless the setter creates one. - An inherited non-writable data property. The assignment fails. In sloppy mode it fails silently. In strict mode, which includes ES modules and class bodies, it throws a
TypeError.
const base = {
set label(v) { this._label = v.toUpperCase(); }
};
Object.defineProperty(base, 'id', { value: 1, writable: false });
const item = Object.create(base);
item.label = 'new';
console.log(Object.hasOwn(item, 'label')); // false (the setter ran)
console.log(item._label); // "NEW" (own property made by the setter)
try {
item.id = 99; // throws in strict mode, ignored otherwise
} catch (e) {
console.log(e instanceof TypeError); // true in strict mode
}
console.log(item.id); // 1If you need an own property anyway, use Object.defineProperty(item, 'id', { value: 99 }). It defines the property directly on the object and skips both checks.
Common errors
| Mistake | What actually happens |
|---|---|
| Expecting `child.x = 1` to update the prototype | A new own property shadows it. Siblings still see the old value. |
| Using `delete child.x` to reset an inherited value | It works only if `child` has its own `x`. The prototype keeps its value. |
| Assuming every write creates an own property | Inherited setters and non-writable properties take over. |
| Mutating an inherited object, as in `child.list.push(1)` | This is a read followed by a mutation, not an assignment, so nothing is shadowed. The pitfall chapter covers it. |
Exercise
Given const p = { n: 1 }; const c = Object.create(p); c.n++;, what are c.n, p.n, and Object.hasOwn(c, 'n')?
Show answer
c.n is 2, p.n is 1, and Object.hasOwn(c, 'n') is true. c.n++ reads the inherited 1, then assigns 2, and that assignment creates an own property on c that shadows p.n.
Exercise
A prototype defines get total() { return 10; } with no setter. In strict mode, what happens when you run child.total = 5?
Show answer
It throws a TypeError. An inherited accessor that has no setter blocks the assignment the same way a non-writable property does. In sloppy mode the write is ignored silently, and child.total stays 10.