2012-02-13 24 views
1

現在、私はすべてのJavaScript関数を1つの関数で使用しているので、グローバル名前空間を汚染しません。jQuery Limitグローバル名前空間

私はjQueryの関数でVARアプリ=関数(){}包装しようとしている

(function (window, document, $) { 
    ... 
}(window, document, jQuery)); 

、しかし:

var App = function() { 

    function a() { 
    } 

    return {}; 
}(); 

今私は私は、これは以下に包まれたいのjQueryを使用したいですこれは、アプリケーションにAJAX設定のプロパティがあるときに問題を引き起こします。何か案は?

答えて

1

document、アプリへの直接jQuery

var App = function(window, document, $) { 
    function a() { 
    } 
}(window, document, jQuery); 

また、あなたグローバル名前空間でAppを宣言できますが、jQueryクロージャで次のように定義します。

var App = null; 

(function (window, document, $) { 
    App = function() { 
     function a() { 
     } 
    }; 
}(window, document, jQuery)); 

これらの例では、return {}を削除し、2番目の例では非自己呼び出し機能を作成しています。構造体のその部分は、コピー・アンド・ペースト・クラフトのように見え、不要です。

0

これはどう:

代わりに別の匿名関数を使用しての、あなただけの windowを渡すことができ
var App = function(window, document, $) { 

     function a() { 
     } 

     return {}; 

}(window, document, jQuery); 
関連する問題