Sub topic 9
Descriptor, Prototype, and Extensibility Traps
This chapter assumes you know property descriptors (value, writable, enumerable, configurable, get, set), prototype-chain lookup, and how Reflect forwards a trap to its default behavior. These six traps come up less often than get and set. They control how an object describes itself, which prototype it reports, and whether it can grow new properties.
The Six Traps at a Glance
| Trap | Internal method | Triggered by |
|---|---|---|
| `defineProperty` | `[[DefineOwnProperty]]` | `Object.defineProperty`, `Reflect.defineProperty`, and assignment when no `set` trap exists |
| `getOwnPropertyDescriptor` | `[[GetOwnProperty]]` | `Object.getOwnPropertyDescriptor`, `Object.keys`, `Object.entries`, spread, and assignment |
| `getPrototypeOf` | `[[GetPrototypeOf]]` | `Object.getPrototypeOf`, `instanceof`, `isPrototypeOf`, `__proto__` |
| `setPrototypeOf` | `[[SetPrototypeOf]]` | `Object.setPrototypeOf`, `Reflect.setPrototypeOf` |
| `isExtensible` | `[[IsExtensible]]` | `Object.isExtensible`, `Object.isFrozen`, `Object.isSealed` |
| `preventExtensions` | `[[PreventExtensions]]` | `Object.preventExtensions`, `Object.freeze`, `Object.seal` |
All six traps return a result to the engine. The boolean ones (defineProperty, setPrototypeOf, preventExtensions) work like set. If the trap returns false, Object.* methods throw a TypeError and Reflect.* methods return false.
defineProperty and getOwnPropertyDescriptor
A defineProperty trap sees every attempt to create or change a property, including plain assignment. If the proxy has no set trap, the default [[Set]] runs on the target with the proxy as receiver, and it ends by calling the receiver's [[GetOwnProperty]] and [[DefineOwnProperty]].
That flow lets a single trap guard both assignment and Object.defineProperty:
'use strict';
const store = new Proxy({}, {
defineProperty(target, prop, descriptor) {
if (typeof prop === 'string' && prop.startsWith('_')) {
return false;
}
return Reflect.defineProperty(target, prop, descriptor);
},
getOwnPropertyDescriptor(target, prop) {
console.log(`describe ${String(prop)}`);
return Reflect.getOwnPropertyDescriptor(target, prop);
}
});
store.visible = 1; // logs "describe visible"
console.log(Reflect.defineProperty(store, '_x', { value: 1 })); // false
try {
store._y = 2; // strict mode: throws
} catch (err) {
console.log(err instanceof TypeError); // true
}getPrototypeOf and setPrototypeOf
getPrototypeOf changes what the object reports as its prototype. instanceof reads the prototype through this trap, so you can control type checks:
class Animal {}
class Robot {
beep() { return 'beep'; }
}
const disguised = new Proxy(new Robot(), {
getPrototypeOf() {
return Animal.prototype;
},
setPrototypeOf() {
return false; // refuse prototype changes
}
});
console.log(disguised instanceof Animal); // true
console.log(disguised instanceof Robot); // false
console.log(disguised.beep()); // 'beep'
console.log(Reflect.setPrototypeOf(disguised, null)); // falseThe last call to beep() shows the main gotcha. Property lookup never consults getPrototypeOf. Without a get trap, the read is forwarded to the target's [[Get]], and that walks the target's real chain. The reported prototype and the prototype used for lookup can therefore disagree.
isExtensible and preventExtensions
These two traps cover whether new properties can be added. Object.freeze and Object.seal both start by calling [[PreventExtensions]], so returning false from the trap makes the object impossible to freeze:
const unfreezable = new Proxy({}, {
preventExtensions() {
return false;
}
});
console.log(Reflect.preventExtensions(unfreezable)); // false
try {
Object.freeze(unfreezable);
} catch (err) {
console.log(err instanceof TypeError); // true
}These traps have very little freedom. isExtensible must return the same answer as Object.isExtensible(target), and preventExtensions can return true only if the target really is non-extensible afterward. In practice you either refuse, as above, or you forward with Reflect and add a side effect such as logging. The next chapter covers these rules and the ones on the descriptor traps.
Exercise
A proxy over { a: 1, b: 2 } has a getOwnPropertyDescriptor trap that returns undefined for 'b' and forwards with Reflect for every other key. It has no other traps. What do Object.keys(proxy), 'b' in proxy, and proxy.b return?
Show answer
Object.keys(proxy) returns ['a'], because Object.keys checks each key's descriptor and skips keys whose descriptor is undefined. 'b' in proxy is true, because [[HasProperty]] is forwarded to the target and does not use the trap. proxy.b is 2, because [[Get]] is forwarded to the target. Hiding a key this way is allowed only because 'b' is configurable and the target is extensible.
Exercise
Without a get trap, why does a getPrototypeOf trap that returns Animal.prototype not let the proxy use methods defined on Animal.prototype?
Show answer
Property reads go through [[Get]], which the proxy forwards to the target. The target's [[Get]] walks the target's actual prototype chain and never calls the proxy's getPrototypeOf trap. To make lookup match the reported prototype, you also need a get trap, for example one that falls back to Reflect.get(Animal.prototype, prop, receiver).