这是我的引导程序:
LanesBoard2010 = (function () {
var appRoot = this;
appRoot.deferredLoad = new Object(); //holding jqxhr states
appRoot.utils = {}; //namespace for helper classes
appRoot.models = {};
appRoot.data = (function () { } ()); //store data from webservices here
appRoot.app = (function () { } ()); //ViewModel
}();
经过一些初始化后,我运行了我的延迟应用程序加载器:
function appStart() {
appRoot.deferredLoad.app = $.getScript('../TaskBoardContent/Script/app/lanesboard.js');
$.when.apply($, appRoot.deferredLoad.app).then(function () {
if (window.console)
if (debug)
console.log('application lanesdashboard was loaded successfully\n');
}, function () {
if (window.console)
if (debug)
console.log('fatal error: unable to load application lanesdashboard\n');
});
};
现在我需要访问 appRoot,或者更一般地说,修改 LanesBoard2010 的属性。
以下语句来自我的 lanesboard.js。它只是在第一个 console.log 之后失败,因为我可以看到两个变量名都是未知的:
(function () {
if (window.console)
if (true) {
console.log('App has been initialised successfully');
console.log('Status of app access 1: ' + appRoot);
console.log('Status of app access 2: ' + LanesBoard2010);
}
}());
听起来可能很愚蠢,但我怎样才能安全地访问我的变量?有没有最佳实践?你会怎么做?