1 前言
2 模块化的基本概念
2.1模块化
模块化
是指解决一个复杂问题
时,自顶向下逐层把系统划分成若干模块的过程
。对于整个系统来说,模块是可组合、可分解、可更换的单位
。- 编程领域中的模块化,就是遵
循固定规则
,把一个大文件
拆成独立并相互依赖
的多个小模块
。
模块化的好处
- 提高代码的
复用性
- 提高代码的
可维护性
- 可以实现
按需加载
2.2 模块化规范
模块化规范
就是对代码进行模块化拆分、组合时,需要遵循的规则。
- 使用什么样的语法格式来
引用模块
- 使用什么样的语法格式来
向外暴露成员
模块化规范的好处
大家都遵循同样的模块化规范写代码,降低了沟通成本,极大方便了各个模块之间的相互调用,利人利己。
3 node 中的模块化
3.1 node 中模块化的分类
Node.js 根据模块的来源不同,将模块分为三大类,分别是:
- 内置模块:由 Node.js 官方提供的,如 fs 模块、 path 模块、 http 模块
- 自定义模块:用户创建的每一个 .js 文件,都是自定义模块
- 外部模块:由第三方开发出来的模块,非 Node.js 官方提供的内置模块,也不是用户创建的自定义模块,使用前需要先下载
3.2 加载模块
使用强大的 require() 方法,可以加载需要的内置模块、自定义模块、外部模块进行使用
// 加载内置模块
const http = require('http')
// 加载自定义模块
const custom = require('./custom.js')
// 加载外部模块
const moment = require('moment')
注意:
- 使用 require() 方法加载其他模块时,会执行被加载模块中的代码
- 在使用 require() 加载用户自定义模块期间,可以省略 .js 的后缀名
4 node 中的模块作用域
4.1 什么是模块作用域
和函数作用域
类似,自定义模块中定义的变量
、方法
等成员,只能在当前模块被访问
,这种模块级别的访问限制
,叫做模块作用域
。
4.2 模块作用域的好处
防止全局变量污染问题
4.3 向外共享模块作用域中的成员
在每个 .js 自定义模块中都有一个 module 对象,它里面存储了和当前模块有关的信息
4.3.1 module.exports 对象
- 在自定义模块中,可以使用 module.exports 对象,将模块内的成员共享出去,供外界使用。
- 外界用 require() 方法导入自定义模块时,得到的就是 module exports 所指向的对象
共享成员时的注意:
使用 require() 方法导入模块时,导入的结果,永远以 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 对象
- 由于 module.exports 单词写起来比较复杂,为了简化向外共享成员的代码, Node.js 提供了 exports 对象。
- 默认情况下,exports 和 module exports 指向同一个对象,最终共享的结果,还是以 module.exports 指向的对象为准。
4.3.3 exports 和 module exports 的使用误区
时刻谨记, require() 模块时,得到的永远都是 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 node 中的模块化规范
Node.js 遵循了 CommonJS 模块化规范, CommonJS 规定了模块的特性
和各模块间如何相互依赖
。
CommonJS 规定:
- 每个模块内部,
module 变量
代表当前模块 - module 变量是一个对象,它的 export 属性(即
module.exports
)是对外的接口 - 加载某个模块,其实是加载该模块 module.exports 属性。 require() 方法用来加载模块
5 es6 module
ES6 在语言标准的层面上,实现了模块功能,旨在替代 AMD 、 CommonJs 成为浏览器和服务器通用的模块解决方案。
ES6 Module 规定:
- 每个 js 文件都是一个独立的模块
- 按需导入: import ,命令用于输入其他模块提供的功能
- 按需导出 : export ,命令用于规定模块的对外接口