(function(){
//this is to access this a property "PublicItem" from outside this closure
window.PublicItem=function(){
//codes
}
//internal variables
var _varA,_varB,_varC,_varD;
//internal functions
function _internalFunc1(){}
function _internalFunc2(){}
function _internalFunc3(){}
})();
情況B
(function(){
//this is to access this a property "PublicItem" from outside this closure
window.PublicItem=function(){
//codes
}
//this variable will hold all internal variables and methods
var _global={};
//internal variables to hold all other internal variables
_global._varA="something for a";
_global._varB="something for b";
_global._varC="something for c";
//internal functions
_global._internalFunc1=function(){}
_global._internalFunc2=function(){}
_global._internalFunc3=function(){}
})();
兩種情況之間的區別在於情形A我已經多達變量/函數創建爲我需要。並在case2我創建了一個名爲_global
的變量,並創建任何內部變量/函數我已經使它的屬性_global
。我想讓你建議我在記憶和表現的意義上哪種方式更好,你也可以評論任何其他參數。
oops只是犯了一個小錯誤糾正它! –
您的匿名函數表達式在語法上是錯誤的。他們應該看起來像'(function(){/ * ... code here ... * /})();' –
@andy和@felix,只是糾正它。 –