有没有简单的方法来断言对象是代理的目标对象?ES2015代理服务器:断言代理服务器与目标对象相同
const o = {};
const p = new Proxy(o, {});
平等运营商似乎并不工作,as outlined in this paper on page 6:
o == p; // false
o === p; // false
const q = new Proxy(o, {});
p === q; // false
有什么办法来验证它们指向同一个对象,然后其他字符串化和重新分析对象?
典型的用例:
我想比较非与代理节点代理之一。由于我自己创建了代理,因此我知道应该预期哪些行为。
const nodeList = new Proxy(document.querySelectorAll('div'), {
get(target, key) { return new Proxy(target[key], {}); }
});
const specificNode = document.querySelector('div[data-target]');
for (const node of nodeList) {
if (node === specificNode) { doSomethingElse(); } // Never happens, even if specificNode is in the nodeList
}
你为什么要在意?如果你不知道代理的作用,它可能表现得像“目标”或绝对不行。 – Bergi
@Bergi请参阅上面的示例用例。 – nils
为什么不宁愿'if(“target”in node.dataset)... else'?当然,创建内部代理没有任何陷阱是毫无意义的。 – Bergi