Generally speaking, the module is a separate JS file. A module file can contain a class definition, a set of related classes, a utility library, or some code to execute. The goal of modularity is to support large-scale program development, to handle the assembly of code in a decentralized source, and to allow code to run correctly, even if it contains unnecessary module code, and can execute code correctly.
by defining the module within a function, the defined variables and functions belong to the function's local variables and are not visible outside the function. In fact, this function scope can be used as a module namespace (module function)
Once the module code is encapsulated in a function, some methods are required to export the public API to invoke them outside of the module functions. There are several ways to export a common API:
first create a namespace
Create a global variable to hold school-related modules
var School; Create a school namespace
if (!school) school = {};
1. Using the constructor function
650) this.width=650; "src=" Http://183.61.143.148/group1/M00/01/F3/tz2PlFQEJoHzS0tQAAAxgZ_M63g437.png "height=" 200 "Width=" 537 "/>
650) this.width=650; "src=" Http://183.61.143.148/group1/M00/01/F3/tz2PlFQEJp_RQIevAAAoAy4vl00297.png "/>
2. Call through the keyword new
Another similar technique is to call a module function as a constructor, via new. Assign them (public API) to the This property to export them
650) this.width=650; "src=" Http://183.61.143.148/group1/M00/01/F3/tz2PlFQEJrmAqeQvAAAf7GmG1s4107.png "/>
3. namespace objects are defined
as an alternative, if a global namespace object has been defined, the properties of that object can be set directly through the module function.
650) this.width=650; "src=" Http://183.61.143.148/group1/M00/01/F3/tz2PlFQEJtnjBGgvAAAqc_XhaL4046.png "/>
The way to export the public API has been finished.
This article is from the "Add Language" blog, please make sure to keep this source http://yuguotianqing.blog.51cto.com/9292883/1547559
JavaScript modularity and namespaces