0

これは私のブートストラップです:

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);

        }
}());

ばかげているように聞こえるかもしれませんが、変数に安全にアクセスするにはどうすればよいですか? ベストプラクティスはありますか? これをどのように行いますか?

4

1 に答える 1

0

あなたの質問は正確には何ですか?未定義の変数の使用を防ぐにはどうすればよいですか?

呼び出しの前に定義して初期化するnull( var appRoot = LanesBoard2010 = null) か、呼び出す前に変数をチェックするconsole.log()( ) ことができますif (appRoot) console.log('Status of app access 1: ' + appRoot);

私は通常、最初の提案を使用します。最初にすべてのキャッシュを に設定しnull、オートコンプリートを作成してから、 を使用$.ajaxしてそれらをロードし、ロードされたらオートコンプリート ソースを更新します。

于 2013-03-06T16:39:21.177 に答える