检查代理对象是否被撤销

Check if proxy object is revoked

ECMAScript 6 引入了代理对象,它可以被创建为可撤销的。

如何检测代理是否已被撤销?

Proxy 构造函数只接受目标和处理程序,当它们是对象且未被撤销代理时。来自 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.

这允许您检查一个值是否是已撤销的代理:您只需要 ensure that it's an objectProxy 抛出。

像这样的东西应该可以工作:

function isRevokedProxy(value) {
  try {
    new Proxy(value, value);
    return false;
  } catch(err) {
    return Object(value) === value;
  }
}