Proxy.revocable() - JavaScript 编辑

The Proxy.revocable() method is used to create a revocable Proxy object.

Syntax

Proxy.revocable(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 define the behavior of proxy p when an operation is performed on it.

Return value

A newly created revocable Proxy object is returned.

Description

A revocable Proxy is an object with following two properties {proxy: proxy, revoke: revoke}.

proxy
A Proxy object created with new Proxy(target, handler) call.
revoke
A function with no argument to invalidate (switch off) the proxy.

If the revoke() function gets called, the proxy becomes unusable: Any trap to a handler will throw a TypeError. Once a proxy is revoked, it will remain revoked and can be garbage collected. Calling revoke() again has no effect.

Examples

Using Proxy.revocable

var revocable = Proxy.revocable({}, {
  get: function(target, name) {
    return "[[" + name + "]]";
  }
});
var proxy = revocable.proxy;
console.log(proxy.foo); // "[[foo]]"

revocable.revoke();

console.log(proxy.foo); // TypeError is thrown
proxy.foo = 1           // TypeError again
delete proxy.foo;       // still TypeError
typeof proxy            // "object", typeof doesn't trigger any trap

Specifications

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

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

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

词条统计

浏览:105 次

字数:3428

最后编辑:7年前

编辑次数:0 次

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