Article directory
1 Introduction
The content of this article comes from bilibili dark horse programmers
2 The basic concept of modularization
2.1 Modularity
模块化
It means that when solving one复杂问题
, go from top to bottom layer by layer把系统划分成若干模块的过程
. For the whole system, the module is可组合、可分解、可更换的单位
.- Modularization in the field of programming is to follow
循固定规则
and大文件
disassemble one into独立并相互依赖
another多个小模块
.
The benefits of modularity
- improve the code
复用性
- improve the code
可维护性
- can be realised
按需加载
2.2 Modular specification
模块化规范
It is the rules that need to be followed when modularizing and combining code.
- What grammatical format to use to
引用模块
- What kind of grammatical format to use?
向外暴露成员
The benefits of modularization.
Everyone writes code following the same modularization specification, which reduces communication costs and greatly facilitates mutual calls between modules, benefiting others and themselves.
3 Modularity in node
3.1 Classification of modularity in node
According to different sources of modules, Node.js divides modules into three categories, namely:
- Built-in modules: Officially provided by Node.js, such as fs module, path module, http module
- Custom module: Every .js file created by the user is a custom module
- External modules: modules developed by third parties, not built-in modules officially provided by Node.js, nor custom modules created by users, need to be downloaded before use
3.2 Loading modules
Using the powerful require() method, you can load the required built-in modules, custom modules, and external modules for use
// 加载内置模块
const http = require('http')
// 加载自定义模块
const custom = require('./custom.js')
// 加载外部模块
const moment = require('moment')
Notice:
- When using the require() method to load other modules, the code in the loaded module will be executed
- When using require() to load user-defined modules, the suffix of .js can be omitted
4 Module scope in node
4.1 What is module scope
And similarly, the , etc. members 函数作用域
defined in custom modules , , such , are called .变量
方法
只能在当前模块被访问
模块级别的访问限制
模块作用域
4.2 Benefits of module scope
Prevent global variable pollution problems
4.3 Sharing members of module scope outwards
There is a module object in each .js custom module, which stores information related to the current module
4.3.1 module.exports object
- In a custom module, you can use the module.exports object to share the members of the module for external use.
- When the outside world uses the require() method to import a custom module, what is obtained is the object pointed to by module exports
Note when sharing members:
When using the require() method to import a module, the result of the import is always subject to the object pointed to by module exports
// m1.js
const userName = 'zs'
module.exports.userName = userName
module.exports.sayHello = () => console.log('hello');
module.exports = {
userName: 'ls',
sayHi: () => console.log('hello')
}
// test.js
const m1 = require('./m1')
console.log(m1) // { userName: 'ls', sayHi: [Function: sayHi] }
// m1.js
const userName = 'zs'
module.exports = {
userName: 'ls',
sayHi: () => console.log('hello')
}
module.exports.userName = userName
module.exports.sayHello = () => console.log('hello');
// test.js
const m1 = require('./m1')
console.log(m1) // {
// userName: 'zs',
// sayHi: [Function: sayHi],
// sayHello: [Function (anonymous)]
// }
4.3.2 exports object
- Since the word module.exports is complicated to write, in order to simplify the code of sharing members, Node.js provides the exports object.
- By default, exports and module exports point to the same object, and the final shared result is still based on the object pointed to by module.exports.
4.3.3 Misuse of exports and module exports
Always keep in mind that when you require() a module, you will always get the object pointed to by module.exports:
exports.userName = 'zs'
module.exports = {
userName: 'ls',
age: 23
}
// { userName: 'ls', age: 23 }
module.exports.userName = 'zs'
exports = {
userName: 'ls',
age: 23
}
// { userName: 'zs' }
exports.userName = 'zs'
module.exports.age = 23
// { userName: 'zs', age: 23 }
exports = {
userName: 'zs',
}
module.exports = exports
module.exports.age = 23
// { userName: 'zs', age: 23 }
4.4 Modular specification in node
Node.js follows the CommonJS modular specification, and CommonJS specifies 模块的特性
and 各模块间如何相互依赖
.
CommonJS states:
- Inside each module,
module 变量
representing the current module- The module variable is an object whose export property (ie
module.exports
) is the external interface- Loading a module is actually loading the module.exports property of the module. The require() method is used to load modules
5 es6 module
ES6 implements module functions at the level of language standards, aiming to replace AMD and CommonJs as common module solutions for browsers and servers.
ES6 Module specifies:
- Each js file is an independent module
- Import on demand: import , the command is used to import functions provided by other modules
- Export on demand: export , the command is used to specify the external interface of the module