The module mode is a block of code that provides public and private methods, facilitates encapsulation of organization code, and reduces the conflict between variables and function names against other modules.
Recommended reading:
Http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html
http://toddmotto.com/mastering-the-module-pattern/
The module model is varied and I only list the module patterns that are commonly used, or are relatively perfect or have special uses.
1. Revealing module mode with private variable plus "_"
var Module = (function () { varfunction () { // Private Stuff }; var function () { _privatemethod (); }; return { publicmethod:publicmethod };}) ();
All functions are placed inside the function, and the private function or variable is preceded by "_", and the public method uses the same name as the return object to directly reference the function inside. This is a very clear structure.
2. Mode of Extension Module
var module = (function (Module) { function () { // Another method ! }; return Module; | | {});
The module can be extended by introducing the previous module.
JavaScript design mode--constructor