Check if proxy object is revoked

﹥>﹥吖頭↗ 提交于 2019-12-12 03:39:45

问题


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

How can I detect if a proxy has been revoked?


回答1:


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;
  }
}


来源:https://stackoverflow.com/questions/39335909/check-if-proxy-object-is-revoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!