揭示式模块模式Revealing Module Pattern
类似于模块模式,不过将模块的方法和变量变为私有的,只有在需要显式暴露时才揭示,主要焦点是方法上:
var MyModule = ( function( window, undefined ) { function myMethod() { alert( 'my method' ); } function myOtherMethod() { alert( 'my other method' ); } // explicitly return public methods when this object is instantiated return { someMethod : myMethod, someOtherMethod : myOtherMethod }; } )( window ); // example usage MyModule.myMethod(); // undefined MyModule.myOtherMethod(); // undefined MyModule.someMethod(); // alerts "my method" MyModule.someOtherMethod(); // alerts "my other method"
|
代码中myMethod和myOtherMethod两个方法是私有的,不能被外部方法,只有通过专门的someMethod和someOtherMethod才可以。