Proxy.revocable() method is used to create a revocable Proxy 对象。

句法

Proxy.revocable(target, handler);
					

参数

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.

返回值

A newly created revocable Proxy object is returned.

描述

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

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

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.

范例

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
					

规范

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

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
revocable Chrome 63 Edge 12 Firefox 34 IE No Opera 50 Safari 10 WebView Android 63 Chrome Android 63 Firefox Android 34 Opera Android 46 Safari iOS 10 Samsung Internet Android 8.0 nodejs 6.0.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: