Proxy()
constructor is used to create
Proxy
对象。
new Proxy(target, handler)
target
Proxy
. It can be any sort of object, including a native array, a function, or even another proxy.
handler
An object whose properties are functions that define the behavior of the proxy when an operation is performed on it.
使用
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 proxy
handler
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 customise specific aspects of the proxy's behavior. For example, by defining
get()
you can provide a customised 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()
new
operator.
handler.defineProperty()
Object.defineProperty
.
handler.deleteProperty()
delete
operator.
handler.get()
A trap for getting property values.
handler.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptor
.
handler.getPrototypeOf()
Object.getPrototypeOf
.
handler.has()
in
operator.
handler.isExtensible()
Object.isExtensible
.
handler.ownKeys()
Object.getOwnPropertyNames
and
Object.getOwnPropertySymbols
.
handler.preventExtensions()
Object.preventExtensions
.
handler.set()
A trap for setting property values.
handler.setPrototypeOf()
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: function(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"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Proxy constructor' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Proxy()
构造函数
|
Chrome 49 | Edge 12 | Firefox 18 | IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 | Firefox Android 18 | Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持