Module (package)
The introduction of the shortcomings of traditional js
The entire page, we wrote a js file, all of the special effects are on the inside
The coupling is too high, too strong correlation between the code, repairing troubles
Global variables easily contaminated
If the complex js files split into multiple functional js file
Disadvantages:
The occurrence of excessive request, dependent fuzzy, difficult to maintain
This is the traditional pattern of development, it has been abandoned out
So we use a modular development model
Why Modular
Reducing correlation between the code
Easy to deploy
Better separation, demand loading, avoid naming conflicts, easy to maintain
What is modular
Specification own node (block) Specification CommonJS
node -v detection mode version
node is the JavaScript environment, you can run the file in the node js inside, dry server command: node file name
commonjs module, is standardized operation node in the server, not the browser, if you use the browser side, you need to use the file packaged compilation (reference tool browserify webpack gulp, etc.)
The nature of the object is exposed
Each js file, must be exposed to the external interfaces
When writing module, the external exposed interfaces, modele.exports = {} or exports.xxx =
Injection module require () parameters: Path Path Note: If the current file, must be preceded by ./. If you just write a file name, the file is in node_modules
The nature of the object is exposed commonjs a corner of exports
module.exports = {} and exports.xxx = difference?
Both exposure is essentially the same, are exposed to a former target exports of exports will be the memory address of the original cover, which will be added in the original on exports
commonjs is loaded synchronously, the node is the norm, synchronous load in the browser is a pit, as long as a link stuck behind would not be able to perform, it is not recommended, if you must use, should you need to edit package
For web end, each js is a module, each module must be exposed to an interface, each js file, there is a global require () method for introducing module
Modules:
1.node carrying packages (modules), path url fs
2. Third-party modules weui / jquery / axios / zepto
3. Custom modules: we write
Configuration module description file, npm iinit generates a package file package.json
version: The version number
desciption: Description
main: index.js documents we file index.js main file, and the rest are sub-module
In the browser, said require: is not defined
Description browser does not support commonjs, you need to learn packing tools
1.browserify