Proxy() constructor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
TheProxy() constructor createsProxy objects.
In this article
Syntax
new Proxy(target, handler)Parameters
Description
Use theProxy() constructor to create a newProxy object.This constructor takes two mandatory arguments:
targetis the object for which you want to create the proxyhandleris the object that defines the custom behavior of the proxy.
An empty handler will create a proxy that behaves, in almost all respects, exactly likethe target. By defining any of a set group of functions on thehandlerobject, you can customize specific aspects of the proxy's behavior. For example, bydefiningget() you can provide a customized version of the target'sproperty accessor.
Handler functions
This section lists all the handler functions you can define. Handler functions aresometimes calledtraps, because they trap calls to the underlying targetobject.
handler.apply()A trap for a function call.
handler.construct()A trap for the
newoperator.handler.defineProperty()A trap for
Object.defineProperty.handler.deleteProperty()A trap for the
deleteoperator.handler.get()A trap for getting property values.
handler.getOwnPropertyDescriptor()A trap for
Object.getOwnPropertyDescriptor.handler.getPrototypeOf()A trap for
Object.getPrototypeOf.handler.has()A trap for the
inoperator.handler.isExtensible()A trap for
Object.isExtensible.handler.ownKeys()A trap for
Object.getOwnPropertyNamesandObject.getOwnPropertySymbols.handler.preventExtensions()A trap for
Object.preventExtensions.handler.set()A trap for setting property values.
handler.setPrototypeOf()A trap for
Object.setPrototypeOf.
Examples
>Selectively proxy property accessors
In this example the target has two properties,notProxied andproxied. We define a handler that returns a different value forproxied, and lets any other accesses through to the target.
const target = { notProxied: "original value", proxied: "original value",};const handler = { get(target, prop, receiver) { if (prop === "proxied") { return "replaced value"; } return Reflect.get(...arguments); },};const proxy = new Proxy(target, handler);console.log(proxy.notProxied); // "original value"console.log(proxy.proxied); // "replaced value"Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-proxy-constructor> |
Browser compatibility
See also
- Meta programming guide
Reflect