5.模块化

 

1.编程领域中的模块化

编程领域中的模块化,就是遵守固定的规则,把一个大文件拆成独立并互相依赖的多个小模块。

把代码进行模块化拆分的好处:

  • 提高了代码的复用性
  • 提高了代码的可维护性
  • 可以实现按需加载

2.Node.js 中模块的分类

Node.js 中根据模块来源的不同,将模块分为了 3 大类,分别是:

  • 内置模块(内置模块是由 Node.js 官方提供的,例如 fs、path、http 等)
  • 自定义模块(用户创建的每个 .js 文件,都是自定义模块)
  • 第三方模块(由第三方开发出来的模块,并非官方提供的内置模块,也不是用户创建的自定义模块,使用前需要先下载)

m1.js

1
2
// 当前这个文件,就是一个用户自定义模块
console.log('加载了m1这个用户自定义模块')

test1.js

1
2
3
4
5
6
7
8
// 注意:在使用 require 加载用户自定义模块期间,可以省略 .js 的后缀名
const m1 = require('./m1.js')
console.log(m1)
/**
* 加载了m1这个用户自定义模块
* {}
*/
// m1为一个空对象()

注意:使用 require() 方法加载其它模块时,会执行被加载模块中的代码。

3.模块作用域

和函数作用域类似,在自定义模块中定义的变量、方法等成员,只能在当前模块内被访问,这种模块级别的访问限制,叫做模块作用域。

防止了全局变量污染的问题

module_scope.js

1
2
3
4
5
const username = '张三';

function sayHello() {
console.log('大家好,我是' + username);
}

test2.js

1
2
3
4
const custom = require('./module_scope');

console.log(custom);
// {}

4.向外共享模块作用域中的成员

1.module 对象

在每个 .js 自定义模块中都有一个 module 对象,它里面存储了和当前模块有关的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
console.log(module);

// Module {
// id: '.',
// path: 'F:\\code',
// exports: {},
// filename: 'F:\\code\\10.演示module对象.js',
// loaded: false,
// children: [],
// paths: [
// 'F:\\code\\node_modules',
// 'F:\\node_modules'
// ]
// }

2.module.exports 对象

在自定义模块中,可以使用 module.exports 对象,将模块内的成员共享出去,供外界使用。

外界用 require() 方法导入自定义模块时,得到的就是 module.exports 所指向的对象。

module_exports.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 在一个自定义模块中,默认情况下, module.exports = {}

// 向 module.exports 对象上挂载 username 属性
module.exports.username = 'zs';
// 向 module.exports 对象上挂载 sayHello 方法
module.exports.sayHello = function() {
console.log('Hello!');
}
const age = 20;
module.exports.age = age;

// 让 module.exports 指向一个全新的对象
module.exports = {
nickname: '小明',
sayHi() {
console.log('Hi!')
}
}

test3.js

1
2
3
4
5
6
7
8
// 在外界使用 require 导入一个自定义模块的时候,得到的成员,
// 就是 那个模块中,通过 module.exports 指向的那个对象
const m = require('./module_exports.js');

console.log(m);
// { username: 'zs', sayHello: [Function (anonymous)], age: 20 }

// { nickname: '小明', sayHi: [Function: sayHi] }

注:使用 require() 方法导入模块时,导入的结果,永远以 module.exports 指向的对象为准。

3.exports 对象

由于 module.exports 单词写起来比较复杂,为了简化向外共享成员的代码,Node 提供了 exports 对象。默认情况下,exports 和 module.exports 指向同一个对象。最终共享的结果,还是以 module.exports 指向的对象为准。

在对外共享成员时,向module.exports 或者 exports上挂载属性,结果是相同的

exports_object.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
console.log(exports)
console.log(module.exports)

console.log(exports === module.exports)
// true

const username = 'zs'

module.exports.username = username
exports.age = 20
exports.sayHello = function() {
console.log('大家好!')
}

// 最终,向外共享的结果,永远都是 module.exports 所指向的对象

test4.js

1
2
3
const m = require('./exports_object.js')
console.log(m)
// { username: 'zs', age: 20, sayHello: [Function (anonymous)]

4.exports 和 module.exports 的使用对比

require() 模块时,得到的永远是 module.exports 指向的对象:

1
2
3
4
5
6
7
exports.username = 'zs';

module.exports = {
geneder: 'male',
age: 22
}
// { geneder: 'male', age: 22 }
1
2
3
4
5
6
7
module.exports.username = 'zs';

exports = {
geneder: 'male',
age: 22
}
// { username: 'zs' }
1
2
3
exports.username = 'zs';
module.exports.geneder = 'male';
// { username: 'zs', geneder: 'male' }
1
2
3
4
5
6
7
exports = {
username: 'zs',
geneder: 'male'
};
module.exports = exports;
module.exports.age = 22;
// { username: 'zs', geneder: 'male', age: 22 }

注:为了防止混乱,建议不要在同一个模块中同时使用 exports 和 module.exports

Node.js 中的模块化规范

Node.js 遵循了 CommonJS 模块化规范,CommonJS 规定了模块的特性和各模块之间如何相互依赖。

CommonJS 规定:

  • 每个模块内部,module 变量代表当前模块。
  • module 变量是一个对象,它的 exports 属性(即 module.exports)是对外的接口。
  • 加载某个模块,其实是加载该模块的 module.exports 属性。require() 方法用于加载模块。