Sub topic 3
Descriptor, Prototype, and Extensibility Traps
This chapter assumes you know the get, set, has, deleteProperty, and ownKeys traps, and how to forward an operation with Reflect.
The Remaining Traps
Each of these traps maps to one internal method. Each one has a Reflect method with the same name that you can use to forward it.
| Trap | Triggered by |
|---|---|
| `defineProperty` | `Object.defineProperty` and assignments that create a property |
| `getOwnPropertyDescriptor` | `Object.getOwnPropertyDescriptor`, `Object.keys` filtering |
| `getPrototypeOf` / `setPrototypeOf` | `Object.getPrototypeOf`, `instanceof` |
| `isExtensible` / `preventExtensions` | `Object.isExtensible`, `Object.freeze` |
Invariants: Rules Traps Cannot Break
After each trap runs, the engine compares its result with the target. If a trap lies about a non-configurable property or a non-extensible target, the engine throws a TypeError.
const frozen = Object.freeze({ x: 1 });
const liar = new Proxy(frozen, { get: () => 2 });
try {
liar.x;
} catch (err) {
console.log(err instanceof TypeError); // true
}Revocable Proxies
Proxy.revocable returns a proxy together with a revoke function. Once you call revoke, every operation on the proxy throws. Use this when you hand an object to code whose access you may need to withdraw later.
const { proxy, revoke } = Proxy.revocable({ a: 1 }, {});
console.log(proxy.a); // 1
revoke();
try { proxy.a; } catch (err) { console.log(err.message); }Practical Patterns
Validation, logging, and property hiding all follow the same shape. The trap checks or records something, then calls Reflect. A default-value wrapper works the same way:
const withDefaults = (obj, fallback) => new Proxy(obj, {
get(target, prop, receiver) {
return Reflect.has(target, prop)
? Reflect.get(target, prop, receiver)
: fallback;
}
});
const config = withDefaults({ port: 8080 }, 'unset');
console.log(config.port, config.host); // 8080 'unset'Where Proxies Fall Short
- Internal slots:
Map,Set,Date, and similar built-ins keep their data in internal slots, and the proxy does not have those slots. You have to bind their methods to the target. - Private fields:
#fieldlookups fail whenthisis the proxy, for the same reason. - Identity:
proxy !== target. As a result, aWeakMaporSetkeyed on the target will not match the proxy. - Performance: every trapped operation costs an extra function call. Avoid proxies in hot loops.
const map = new Map();
const working = new Proxy(map, {
get(target, prop) {
const value = Reflect.get(target, prop, target);
return typeof value === 'function' ? value.bind(target) : value;
}
});
working.set('a', 1);
console.log(working.get('a'), working.size); // 1 1Exercise
A class stores data in a private field #count and exposes increment(). Calling proxy.increment() on a plain new Proxy(instance, {}) throws a TypeError. Why, and what is one fix?
Show answer
The method runs with this set to the proxy, and the proxy does not have the private field, so the #count lookup fails. One fix is a get trap that binds functions to the target: typeof v === 'function' ? v.bind(target) : v.
Exercise
Will a get trap that returns 5 for every property throw when it reads x on a proxy of Object.freeze({ x: 1 })? What about the same proxy over { x: 1 }?
Show answer
On the frozen target, yes. x is non-writable and non-configurable, so the trap must report its real value. On the unfrozen target, no. The trap can return any value there.