假設你有一個全球性的功能警報2:可能在另一個窗口的上下文中調用Javascript方法?
function alert2(msg) {
window.alert(msg);
}
而且你還需要第二個窗口對象的引用:
childWindow = window.open(myUrl);
現在,你想從窗口在childWindow的情況下調用警報2 :
alert2.call(childWindow, "does not work without this.window");
該對話框出現在主窗口中,因爲alert2中的「窗口」被綁定到該方法定義的窗口d(父窗口)。
一種解決方案是修改警報2:
function alert2(msg) {
this.alert(msg);
}
是否有可能做到這一點沒有這種修改?類似這樣的:
alert2.call(childWindow.parent, "no such thing as window.parent");
這是一個人爲的例子; childWindow.alert(「」)不是我要找的!
我的來源可以看到和修改上的jsfiddle開始http://jsfiddle.net/hJ7uw/2/
謝謝...你的代碼說明我對問題的理解。在這種情況下,無法在上面的func()中替換「child」或「parent」的含義。類似地,似乎無法在函數中替換「窗口」的含義,因爲在創建alert2時捕獲「窗口」,正如在定義func時捕獲「父」和「子」一樣。 –