Proxy() constructor - JavaScript 编辑

The Proxy() constructor is used to create Proxy objects.

Syntax

new Proxy(target, handler)

Parameters

target
A target object to wrap with 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.

Description

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 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.

Handler functions

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.

Examples

Selectively proxy property accessors

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"

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'Proxy constructor' in that specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:111 次

字数:7573

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文