Module (package)
First you need to install node.js (go to the official website)
Traditional development
<script src = "app.js"> <script>
entire page to write a js file in which all of the effects are
drawbacks: the correlation between the degree of coupling code is too high, not easy to post-maintenance will cause global pollution
< the src = Script "01.js"> <Script>
<Script the src = "02.js"> <Script>
<Script the src = "02.js"> <Script>
<Script the src = "02.js"> <Script >
Js file references multiple
number of requests that the wording of the drawbacks of this happening too dependent fuzzy, difficult to maintain
Modular Development
What is modular?
A complex process based on certain rules (specification) packaged into several blocks (files), and combined
Internal data block is implemented with a proprietary, just expose some interfaces (methods) to the external communication with other external modules
Why Modular?
-
-
- Avoid naming conflicts (reducing namespace pollution)
- Better separation, demand loading
- Higher reusability
- High maintainability
-
Modular Specification
CommonJS
Overview:
Node Application is composed of modules, using CommonJS module specification. Each file is a module, has its own scope. In a document which defined variables, functions, classes are private, not visible to other documents. On the server side, the load module is loaded run-time synchronization; on the browser side, the module compiler package deal in advance. (Synchronous load: as long as there is a link stuck, not performed on the back)
Features:
-
-
-
- All code runs in the module scope, it does not pollute the global scope.
- Modules can be loaded more than once, but will only run once when you first load, then run the results are cached, and later loaded directly read cache results. To get the module to run again, you must clear the cache.
- Sequentially loaded modules, the order in which they appear in the code.
-
-
The basic syntax:
Module.exports = value exposure module or exports.xxx = value
The require injection module (xxx) If a third module, xxx is the module name; if it is custom module, the file path for the module xxx