What is modularity?
A: A complex program is packaged into several blocks according to certain rules and combined together. Its internal data is private, but it only exposes some interfaces to the outside to communicate with other external modules.
Why modularity?
- Reduce complexity
- The deployment of convenient
- To reduce the coupling
- Avoid naming conflicts
Commonjs modular specification
- This file has two requirements: the package name and the version number.
- The package name cannot contain uppercase or Chinese characters.
- Automatically generate package.json files
npm init
Copy the code
- Install the uniq
npm install uniq
Copy the code
- — What does save mean? Add to package.json
Commonjs modules are exposed in three ways
1: module.exports = {}
2: module.exports = function()
- It is not recommended that the following type overwrite the preceding one
3: exports
- This is recommended by adding an attribute to exports
4: Install using NPM
- Take the uniq package, which filters out unique elements in an array.
- npm install uniq
- Const uniq = require(‘ uniq ‘);
- Const result = uniq(arr);
Commonjs is based on browser-based applications
- First install globally and also install Browserify locally
npm install browserify -g
npm install browserify --save-dev
Copy the code
- The require method cannot be accurately recognized on the browser side
- You need to use Browserify for packaging
- Instead of running our source file, the browser runs a packaged file
- Run successfully at this point