- 文件模块一一对应
引入模块
- require用于从外部获取一个模块
- 引入当前文件夹内的hello.js
var hello = require('./hello');
导出模块
- exports用于把对象公开
- 公开文件中的 'world' function:【hello.js】exports.world = function() {...}
- 使用 world:require('hello'); hello.world() //先import文件,再使用function
- 直接公开一个对象:【hello.js】function Hello() {...} module.exports = Hello;
- 直接使用hello对象:require('hello'); hello.xxx
- 公开文件中的 'world' function:【hello.js】exports.world = function() {...}
**前者require返回的是exports对象,world是其中的成员函数;后者返回的hello对象,也就是我们要的东西
模块加载
文件模块缓存 --> 原生模块 --> 文件系统