Here is a concise front-end knowledge system waiting for you to check, have a look, there will be a surprise oh, if you feel good, begged star ha ~


Module mode Description

The modular pattern adds private variables and private methods to the singleton pattern and reduces the use of global variables; Here is the code structure for a module pattern:

var singleMode = (functionVar privateNum = 112; // Create a private methodfunction privateFunc(){}, // Create a public methodfunction publicMethod1() {},function publicMethod2(){}, // Returns an object containing public methods and attributesreturn{ publicMethod1: publicMethod1, publicMethod2: publicMethod2 }; }) ();Copy the code

As shown above, the module pattern uses an anonymous function that returns an object. Inside this anonymous function, private variables and functions are defined for use by the internal function, and an object literal is returned as the value of the function. The returned object literal contains only properties and methods that can be exposed. In this way, the method can be provided for external use; Because the public methods in this return object are defined inside anonymous functions, it has access to internal private variables and functions.

Module mode usage scenarios

When creating an object that needs to be initialized internally and has limited access to internal properties and methods, you need to use the module mode.