require
- 执行被加载模块中的代码
console.log('a1');
var b=require('./b');
console.log('a2');
console.log('b');
a 文件加载 b文件,获取b内容结果
- 得到被加载模块中的exports对象
exports
exports 是 module.exports 的简写引用;实际导出的是 module.exports。
exports 导出的是对象; module.exports 导出的可以是单个成员变量,也可以是对属性对象。
exports === module.exports (true)
区分:exports 和 module.exports 混用情况,如果 module.exports 结构改变,则 exports 原先赋值无效。
exports.a='这是一个对象';
module.exports='单个成员变量';
module.exports={
a:{'key':'这是一个对象'},
b:'单个成员变量'
}
当 a 文件获取 b 文件内容时
- 只存在 exports 则以对象形式输出
- 存在 module.exports 时,位置无关,输出最后一个 module.exports