1

ECMAScript 6 introduces proxy object, which may be created as revocable.

How can I detect if a proxy has been revoked?

Oriol
  • 274,082
  • 63
  • 437
  • 513

1 Answers1

3

The Proxy constructor only accepts targets and handlers when they are objects and are not revoked proxies. From ProxyCreate,

  1. If Type(target) is not Object, throw a TypeError exception.
  2. If target is a Proxy exotic object and the value of the [[ProxyHandler]] internal slot of target is null, throw a TypeError exception.

This allows you to check if a value is a revoked proxy: you only need to ensure that it's an object but makes Proxy throw.

Something like this should work:

function isRevokedProxy(value) {
  try {
    new Proxy(value, value);
    return false;
  } catch(err) {
    return Object(value) === value;
  }
}
Community
  • 1
  • 1
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • This is really slick, because a revoked proxy has no accessibility, but `typeof (RevokedProxy)` does not trigger a TypeError like any other operation — great solution. – jpschroeder Aug 07 '21 at 00:21