The Proxy()
constructor creates Proxy
objects.
The Proxy()
constructor creates Proxy
objects.
Use the Proxy()
constructor to create a new Proxy
object. This constructor takes two mandatory arguments:
target
is the object for which you want to create the proxyhandler
is the object that defines the custom behavior of the proxy. An empty handler will create a proxy that behaves, in almost all respects, exactly like the target. By defining any of a set group of functions on the handler
object, you can customize specific aspects of the proxy's behavior. For example, by defining get()
you can provide a customized version of the target's property accessor.
This section lists all the handler functions you can define. Handler functions are sometimes called traps, because they trap calls to the underlying target object.
handler.apply()
A trap for a function call.
handler.construct()
A trap for the new
operator.
handler.defineProperty()
A trap for Object.defineProperty
.
handler.deleteProperty()
A trap for the delete
operator.
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 in
operator.
handler.isExtensible()
A trap for Object.isExtensible
.
handler.ownKeys()
A trap for Object.getOwnPropertyNames
and Object.getOwnPropertySymbols
.
handler.preventExtensions()
A trap for Object.preventExtensions
.
handler.set()
A trap for setting property values.
handler.setPrototypeOf()
A trap for Object.setPrototypeOf
.
In this example the target has two properties, notProxied
and proxied
. We define a handler that returns a different value for proxied
, 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"
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
Proxy |
49 | 12 | 18 | 36 | 10 | 49 | 18 | 36 | 10 | 5.0 | 49 | 1.0 | 6.0.0 |
Reflect
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy