Skip to content

Proxy

The Proxy global object wraps another object and lets a handler intercept the basic operations performed on it, such as reading, writing, or deleting a property. Each intercepting function on the handler is called a trap. In this example, a get trap returns a placeholder for a missing translation instead of undefined:

Language: JavaScript
> const labels = new Proxy(
…   { save: "Save" },
…   { get: (target, key) => target[key] ?? `[missing: ${String(key)}]` },
… );
undefined

> labels.save
'Save'
> labels.cancel
'[missing: cancel]'

Any operation without a trap on the handler passes straight through to the target. An empty handler therefore gives a proxy that forwards every operation to its target. Since ES2015, Proxy has shipped with the Reflect namespace, which mirrors each trap so a trap can forward an operation to the target:

A proxy sends each operation to a handler trap if one exists, which can forward it with Reflect, or straight to the target.
Traps Intercept Operations Before They Reach the Target

Syntax

The Proxy constructor takes the object to wrap and a handler object:

Language: JavaScript Syntax
new Proxy(<target>, <handler>)
Parameter Description
target The object to wrap. It can be any object, including an array, a function, or another proxy
handler An object whose methods are the traps. Operations it doesn’t define fall through to target

The constructor returns a new proxy object. It must be called with new, and calling Proxy() without it throws a TypeError. Proxy has no prototype property, so proxies have no instance methods of their own. A proxy reports its target’s prototype unless a getPrototypeOf() trap says otherwise.

Key Static Methods and Properties

Member Kind Description
Proxy.revocable() Static method Returns an object holding a new proxy and a revoke() function that permanently switches the proxy off

Key Handler Traps

Member Kind Description
get() Trap Intercepts reading a property
set() Trap Intercepts assigning a property and returns true on success
has() Trap Intercepts the in operator
deleteProperty() Trap Intercepts the delete operator
ownKeys() Trap Intercepts listing own keys, as in Object.keys() and Reflect.ownKeys()
defineProperty(), getOwnPropertyDescriptor() Trap Intercept defining a property and reading its descriptor
getPrototypeOf(), setPrototypeOf() Trap Intercept reading and changing the prototype
isExtensible(), preventExtensions() Trap Intercept checking and turning off extensibility
apply() Trap Intercepts calling the target, which must be a function
construct() Trap Intercepts calling the target with new

Every trap receives the target as its first argument. Each one matches a Reflect method with the same name and arguments, so a trap can add its own logic and then forward the operation with a call such as Reflect.set(target, key, value).

Examples

An inventory object must only ever hold whole, non-negative stock counts. A set trap returns false to reject a bad value, and strict-mode code, which includes every module, turns that false into a TypeError:

Language: JavaScript Filename: inventory.mjs
const stock = new Proxy(
  {},
  {
    set(target, key, value) {
      if (!Number.isInteger(value) || value < 0) {
        return false;
      }
      return Reflect.set(target, key, value);
    },
  },
);

stock.mugs = 12;
console.log(`Mugs: ${stock.mugs}`);

try {
  stock.tees = -3;
} catch (error) {
  console.log(`Rejected: ${error.message}`);
}

console.log(`Tees: ${stock.tees}`);

Running the module shows the valid count stored and the negative one refused:

Language: Shell
$ node inventory.mjs
Mugs: 12
Rejected: 'set' on proxy: trap returned falsish for property 'tees'
Tees: undefined

In sloppy-mode code, the same rejected assignment fails silently instead of throwing.

An app hands session data to a third-party plugin and wants to cut off its access when the plugin unloads. Proxy.revocable() returns the proxy along with a revoke() function, and after that call every trapped operation on the proxy throws:

Language: JavaScript
> const session = { userId: 42 };
undefined
> const { proxy: pluginView, revoke } = Proxy.revocable(session, {});
undefined

> pluginView.userId
42

> revoke();
undefined
> pluginView.userId
Uncaught TypeError: Cannot perform 'get' on a proxy that has been revoked
> typeof pluginView
'object'

The typeof operator still works because it doesn’t trigger a trap.

Common Gotchas

A proxy is a separate object with its own identity, so it never equals its target, even though writes through it land on the target:

Language: JavaScript
> const config = { debug: false };
undefined
> const watched = new Proxy(config, {});
undefined

> watched === config
false
> watched.debug = true;
true
> config.debug
true

Built-in objects that keep their data in internal slots, such as Map, Set, and Date, don’t work through a plain proxy. Their methods run with the proxy as this, and the proxy doesn’t have those slots:

Language: JavaScript
> const tags = new Proxy(new Map([["js", 3]]), {});
undefined

> tags.size
Uncaught TypeError: Method get Map.prototype.size called on incompatible receiver #<Map>
    at get size (<anonymous>)

The same applies to private class fields. A get trap that reads properties with the target as the receiver and binds methods to the target works around it:

Language: JavaScript
> const tags = new Proxy(new Map([["js", 3]]), {
…   get(target, key) {
…     const value = Reflect.get(target, key, target);
…     return typeof value === "function" ? value.bind(target) : value;
…   },
… });
undefined

> tags.size
1
> tags.get("js")
3

A trap also can’t report a value that contradicts the target’s fixed properties. When a property is non-writable and non-configurable on the target, as on a frozen object, a get trap that returns a different value throws a TypeError.

A developer turns dials on a console with a card reading Title, Players and Minutes while a robot arm lifts a board game from a shelf onto a conveyor marked Find by ID beside an Update button.

Course

Records With Objects

Describe one thing with named fields in a JavaScript object, keep a list of those records, and write functions that select, find by id and update them. Build a board-game shelf helper, then a small records program of your own.

basics js

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 13, 2026