使用它是一种好习惯,而不是这种显示模块模式......
var MyModule = ( function() {
function Method1() { alert( 'method1' ); }
function Method2() { Method1(); alert( 'method2' ); }
function Method3() { Method1(); alert( 'method3' ); }
function Method4() { Method1(); alert( 'method4' ); }
return { Method1 : Method1, // these
Method2 : Method2, // lines
Method3 : Method3, // are
Method4 : Method4 }; // redundant...
} )();
MyModule.Method1();
MyModule.Method2();
...这个细微的变化:
var MyModule = {};
( function() {
var Method1 = MyModule.Method1 = function () { alert( 'method1' ); };
var Method2 = MyModule.Method2 = function () { Method1(); alert( 'method2' ); };
var Method3 = MyModule.Method3 = function () { Method1(); alert( 'method3' ); };
var Method4 = MyModule.Method4 = function () { Method1(); alert( 'method4' ); };
} )();
MyModule.Method1();
MyModule.Method2();
最后是100%一样吗?这会被认为是好的做法吗?