Node.js 入门 —— 模块化

1 前言

本文内容来自bilibili 黑马程序员

2 模块化的基本概念

2.1模块化

  • 模块化是指解决一个复杂问题时,自顶向下逐层把系统划分成若干模块的过程。对于整个系统来说,模块是可组合、可分解、可更换的单位
  • 编程领域中的模块化,就是遵循固定规则,把一个大文件拆成独立并相互依赖多个小模块

模块化的好处

  1. 提高代码的复用性
  2. 提高代码的可维护性
  3. 可以实现按需加载

2.2 模块化规范

模块化规范就是对代码进行模块化拆分、组合时,需要遵循的规则。

  1. 使用什么样的语法格式来引用模块
  2. 使用什么样的语法格式来向外暴露成员
    模块化规范的好处
    大家都遵循同样的模块化规范写代码,降低了沟通成本,极大方便了各个模块之间的相互调用,利人利己。

3 node 中的模块化

3.1 node 中模块化的分类

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

  1. 内置模块:由 Node.js 官方提供的,如 fs 模块、 path 模块、 http 模块
  2. 自定义模块:用户创建的每一个 .js 文件,都是自定义模块
  3. 外部模块:由第三方开发出来的模块,非 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 规定:

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

5 es6 module

ES6 在语言标准的层面上,实现了模块功能,旨在替代 AMD 、 CommonJs 成为浏览器和服务器通用的模块解决方案。

ES6 Module 规定:

  1. 每个 js 文件都是一个独立的模块
  2. 按需导入: import ,命令用于输入其他模块提供的功能
  3. 按需导出 : export ,命令用于规定模块的对外接口

猜你喜欢

转载自blog.csdn.net/weixin_36757282/article/details/128660960