JavaScript Reference ยท JSON, Errors and Reflection
Proxy handler traps
Reference list of all 13 Proxy handler traps (get, set, has, deleteProperty, ownKeys, apply, construct and more), what triggers each and what it must return.
A proxy handler is an ordinary object. Each method on it that is named after an internal operation is a trap: the engine calls it instead of performing the operation on the target. There are thirteen traps, one for each fundamental operation an object supports. Missing traps fall through to the target.
Every trap has a matching method of the same name on Reflect with the same arguments, which makes it easy to forward the default behavior from inside your trap.
const handler = { get(target, key, receiver) { return Reflect.get(target, key, receiver); }, };
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
The traps
| Trap | Triggered by | Must return |
|---|---|---|
get(target, key, receiver) |
obj.key, obj[key], Reflect.get() |
The property value. |
set(target, key, value, receiver) |
obj.key = v, Reflect.set() |
A boolean: true if the assignment succeeded. |
has(target, key) |
key in obj (and with lookups) |
A boolean. |
deleteProperty(target, key) |
delete obj.key |
A boolean. |
ownKeys(target) |
Object.keys(), Object.getOwnPropertyNames(), Object.getOwnPropertySymbols(), Reflect.ownKeys(), for...in |
An array-like of strings and symbols, without duplicates. |
getOwnPropertyDescriptor(target, key) |
Object.getOwnPropertyDescriptor(), and Object.keys() for each key |
A descriptor object or undefined. |
defineProperty(target, key, descriptor) |
Object.defineProperty(), Reflect.defineProperty() |
A boolean. |
getPrototypeOf(target) |
Object.getPrototypeOf(), instanceof, __proto__ |
An object or null. |
setPrototypeOf(target, prototype) |
Object.setPrototypeOf() |
A boolean. |
isExtensible(target) |
Object.isExtensible() |
A boolean that matches the target. |
preventExtensions(target) |
Object.preventExtensions() |
A boolean. |
apply(target, thisArg, args) |
Calling the proxy (needs a callable target) | The call result. |
construct(target, args, newTarget) |
new proxy(...) (needs a constructor target) |
An object. |
Usage notes
receiveris usually the proxy (or an object inheriting from it). Pass it toReflect.get()andReflect.set()so getters and setters see the rightthis.- Invariants limit what traps may claim. A trap cannot report a non-configurable property as missing, hide keys of a non-extensible target, or return a different value for a frozen property. The engine throws a
TypeErrorif you try. ownKeysalone is not enough to hide a key fromObject.entries()and spread: those also read descriptors and values, so implementgetandgetOwnPropertyDescriptorconsistently.setmust returntrue. A falsy result throws in strict mode code.applyandconstructneed the right target. A proxy is callable only if its target is, and constructible only if the target has[[Construct]](functions and classes; not arrow functions or methods).for...inusesownKeysandgetPrototypeOf, plus descriptors to check enumerability.typeof,Array.isArray()andObject.prototype.toStringhave no traps. They look at the target.- No trap for
===,Object.is()orJSON.stringify. The last one works throughget,ownKeysand the descriptors. - Keep traps small. Do the minimum, then delegate to
Reflect.
Browser support
All thirteen traps are supported in every current browser and in Node.js.