JS: Proxy.revocable

By Xah Lee. Date: .

(new in ECMAScript 2015)

What is proxy.revocable

Proxy.revocable is like the Proxy constructor, but it returns a object with two properties

{proxy, revoke}

Example

// example. proxy.revocable

const xobj = { dog: 3 };

const handler = {
 get(xobj, xkey, thisValue) {
  console.log("proxy trap called.");
  return Reflect.get(xobj, xkey, thisValue);
 },
};

const xresult = Proxy.revocable(xobj, handler);

const xproxy_object = xresult.proxy;

// check proxy works
console.log(xproxy_object.dog);
// proxy trap called.
// 3

const xproxy_revoke_function = xresult.revoke;

// revoke the proxy
xproxy_revoke_function();

// proxy no longer works
console.log(xproxy_object.dog);
// error: Uncaught (in promise) TypeError: Cannot perform 'get' on a proxy that has been revoked