Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
const handler = {
  get(target, key, receiver) {
    return Reflect.get(target, key, receiver);
  },
};

Try it

JS Hiding private-looking keys

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Wrapping calls and constructors

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

  • receiver is usually the proxy (or an object inheriting from it). Pass it to Reflect.get() and Reflect.set() so getters and setters see the right this.
  • 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 TypeError if you try.
  • ownKeys alone is not enough to hide a key from Object.entries() and spread: those also read descriptors and values, so implement get and getOwnPropertyDescriptor consistently.
  • set must return true. A falsy result throws in strict mode code.
  • apply and construct need 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...in uses ownKeys and getPrototypeOf, plus descriptors to check enumerability.
  • typeof, Array.isArray() and Object.prototype.toString have no traps. They look at the target.
  • No trap for ===, Object.is() or JSON.stringify. The last one works through get, ownKeys and 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.