W3cubDocs

/JavaScript

Proxy.revocable

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 which define the behavior of the proxy 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

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

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes Yes 34 No Yes 10
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes Yes 34 Yes 10 Yes
Server
Node.js
Basic support 6.0.0

See also

© 2005–2018 Mozilla Developer Network and individual 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/revocable