Sub topic 3
Traps: The Operations a Proxy Can Intercept
This chapter assumes you know how to create a proxy with new Proxy(target, handler) and that every object operation runs through an internal method such as [[Get]] or [[Set]]. It also assumes you know property descriptors and prototypes at a working level.
A handler is a plain object. Each of its methods is called a trap. A trap is a function the engine calls in place of one internal method when that method runs on the proxy. The handler can define at most 13 traps, one for each internal method an object or function has.
How the Engine Picks a Trap
Every operation on a proxy follows one rule. The engine runs the matching internal method on the proxy. That method looks up the trap with the same name on the handler. If the trap exists, the engine calls it. If it does not, the operation goes straight to the target as if no proxy were there.
The engine looks the trap up each time the operation runs. It does not read the handler only once. If you add a trap to the handler after you create the proxy, the trap takes effect on the next operation. Inside a trap, this is the handler.
The Complete Trap List
| Trap | Internal method | Triggered by (examples) |
|---|---|---|
| `get` | `[[Get]]` | `proxy.x`, `proxy[key]`, reads in an inherited lookup |
| `set` | `[[Set]]` | `proxy.x = 1`, assignments in an inherited lookup |
| `has` | `[[HasProperty]]` | `'x' in proxy`, `with` statements |
| `deleteProperty` | `[[Delete]]` | `delete proxy.x` |
| `ownKeys` | `[[OwnPropertyKeys]]` | `Object.keys`, `Reflect.ownKeys`, `for...in` |
| `getOwnPropertyDescriptor` | `[[GetOwnProperty]]` | `Object.getOwnPropertyDescriptor`, `Object.keys` |
| `defineProperty` | `[[DefineOwnProperty]]` | `Object.defineProperty` |
| `getPrototypeOf` | `[[GetPrototypeOf]]` | `Object.getPrototypeOf`, `instanceof` |
| `setPrototypeOf` | `[[SetPrototypeOf]]` | `Object.setPrototypeOf` |
| `isExtensible` | `[[IsExtensible]]` | `Object.isExtensible` |
| `preventExtensions` | `[[PreventExtensions]]` | `Object.preventExtensions`, `Object.freeze` |
| `apply` | `[[Call]]` | `proxy(...)`, `proxy.call(...)` |
| `construct` | `[[Construct]]` | `new proxy(...)` |
The apply and construct traps only work when the target is a function. A plain object target has no [[Call]] method, so apply is never consulted for it, and calling the proxy throws a TypeError.
One Operation Can Fire Several Traps
Syntax and traps do not map one to one. A built-in function often runs several internal methods in sequence, and each one can hit a different trap. The proxy below logs every trap that fires:
const target = { a: 1, b: 2 };
const handler = {
ownKeys(target) {
console.log('ownKeys');
return Reflect.ownKeys(target);
},
getOwnPropertyDescriptor(target, prop) {
console.log(`getOwnPropertyDescriptor ${String(prop)}`);
return Reflect.getOwnPropertyDescriptor(target, prop);
}
};
const proxy = new Proxy(target, handler);
console.log(Object.keys(proxy));
// ownKeys
// getOwnPropertyDescriptor a
// getOwnPropertyDescriptor b
// [ 'a', 'b' ]Object.keys first asks for every own key. Then it asks for each key's descriptor so it can drop non-enumerable keys. Writing only an ownKeys trap therefore covers part of the operation but not all of it. Later chapters build on this when they hide properties. For now, the Reflect calls forward each step to the target. A later chapter covers them.
Common Errors
- Assuming one syntax form means one trap.
Object.freezecallspreventExtensions, thenownKeys, thendefinePropertyfor each key. - Expecting a trap to run for code inside the target. Traps run only for operations performed on the proxy. If a target method uses
thisbound to the raw target, no trap runs. - Defining
applyon an object proxy. The target decides whether the proxy is callable. The handler cannot make a plain object callable.
Exercise
A handler defines only a has trap that always returns false. For a target { x: 1 }, what do 'x' in proxy and proxy.x produce, and why?
Show answer
'x' in proxy is false because the in operator runs [[HasProperty]], which calls the has trap. proxy.x is 1 because a property read runs [[Get]]. The handler has no get trap, so the read goes straight to the target.
Exercise
You add a get trap to a handler object after the proxy has been created. Does the next property read on the proxy use it?
Show answer
Yes. The engine looks up the trap on the handler every time the operation runs, so changes to the handler take effect immediately.