私のアプリケーションは次のような構造になっています
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を調べたときに、期待どおりに定義されている場合でも同じです。
上記と同じことを試しましたが、代わりに単純なグローバル変数を使用しました(つまり、window.object1の代わりにvar object1)...さまざまな場所でobject1とobject2の初期ダミー値を宣言しようとしました...しかし同じ問題に直面します。
グローバル変数にグローバルにアクセスできない理由を誰かが知っていますか?