LouisTsang-jk.github.io

Module

模块化就是将一个复杂的程序依据一定的规则封装成几个块(文件),并将其组合在一起。
块内部数据和实现是私有的,是指向外暴露一些接口(方法)以此来与其他模块通信。

CommonJS

使用同步的方式加载模块。使用全局性方法require()来加载模块。

// main.js
const circle = require('./circle.js');
console.log(`The area of a circle of radius 4 is ${circle.area(4)}`);
// circle.js
const { PI } = Math;
exports.area = (r) => PI * r ** 2;
exports.circumference = (r) => 2 * PI * r;

流程