【发布时间】:2023-02-08 03:04:56
【问题描述】:
假设我有一个对象 OtherObj 在窗口 OtherWindow 中创建,它不同于当前的 ThisWindow:
const ThisWindow = window;
const ThisObj = ThisWindow.history;
const OtherWindow = window.open();
const OtherObj = OtherWindow.history;
console.log(ThisObj instanceof Object); //true
console.log(OtherObj instanceof Object); //false
console.log(OtherObj instanceof OtherWindow.Object); //true, but this works only if I already have a reference to OtherWindow
现在想象一下,如果我仅有的有 OtherObj 的引用,有没有办法获取用于创建它的窗口?也许OtherObj 上有一个属性保存了对创建它的窗口的引用?
我目前正在尝试想出一种使用 instanceof 运算符的跨窗口方式。正如您在代码示例中看到的,如果变量指向在当前窗口之外创建的对象,[variable] instanceof Object 将返回false。
你们中的一些人可能会说只使用 OtherObj instanceof OtherWindow.Object(它返回 true),但这只有在我已经有对 OtherWindow 的引用时才有效。我的问题是假设我还没有对OtherWindow 的引用。
OtherObj 上的某处是否有指向创建它的窗口的属性?
【问题讨论】:
标签: javascript