var myModule = { myProperty: 'someValue', //here another object is defined for configuration purposes: myConfig: { useCaching: true, language: 'en' }, //a very basic method myMethod: function() { console.log('I can haz functionality?'); }, //output a value based on current configuration myMethod2: function() { console.log('Caching is: ' + (this.myConfig.useCaching) ? 'enabled' : 'disabled'); }, //override the current configuration myMethod3: function(newConfig) { if (typeof newConfig == 'object'){ this.myConfig = newConfig; console.log(this.myConfig.language); } } };
####The module pattern The module pattern is used to further emulate the concept of classes in such a way that we’re able to include both public/private methods and variables inside a single object, thus shielding particular parts from the global scope. What this results in is a reduction in the likelihood of your function names conflicting with other functions defined in additional scripts on the page.
The key to encapsulates “privacy” is to use closures.
Within the module pattern, variables or methods declared are only available inside the module itself thanks to closure.
A simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var testModule = (function(){ //private variable var counter = 0; return { incrementCounter: function() { return counter++; }, resetCounter: function() { console.log('counter value prior to reset: ' + counter); counter = 0; } }; })();
When the setupModuleLoader() function is called, it will return a object, which is window[angular][module], e.g. window.angular.module
window.angular.module is a function itself, the function signiture is : module(name, requires, configFn)
when you call angularModule, you acturally are calling window.angular.module, like module(name, requires, configFn). This function will return modules[name] = moduleInstance
##The Revealing Module Pattern First see an example: