我的應用程序有類似如下結構問題訪問全局JavaScript變量
window.object1;
window.object2;
$(document).ready(function() {
window.object1 = new type1object();
});
function type1object() {
//lots of code
this.property = 'property';
window.object2 = new type2object();
}
function type2object() {
//lots of code
this.property = new type3object();
}
function type3object() {
//lots of code
console.log(window.object1);
this.property = window.object1.property;
}
的問題是,每當我試圖從任何地方訪問其他比window.object1文檔準備回調它回來爲未定義,這即使當我檢查DOM window.object1是完全按照我的預期定義的。
我已經嘗試做與上面相同,但使用簡單的全局變量(即var object1,而不是window.object1)...試圖聲明初始虛擬值object1和object2在各地...但跑起來反對同樣的問題。
有誰知道爲什麼我不能全局訪問我的全局變量?
你執行'type2object()'前'$(文件)。 ready'執行並初始化'window.object1'? – 2009-10-21 09:40:22
沒有。以上是我的代碼的一個非常簡化的版本,但一切都按照 – wheresrhys 2009-10-21 09:46:40
@wheresrhys的順序發生 - 它在Firebug中適用於我。你能夠將函數'type1object'和'type2object'內的代碼給出嗎? – 2009-10-21 09:49:20