CommonJS在做导出操作时有两种方法,分别为:
- module.exports
- exports
在js页面进行打印操作
console.log(module.exports===exports)常会出现True的结果,这时会误认为exports是module中的一种属性.
其实只是在loader.js文件中包含一个代码export=smodule.exports,当js代码中并未对module.exports进行赋值等操作时,且exports未改变指向,上述的打印结果不会发生改变。
let name = "aa";
const key = 10;
function say() {
console.log(this.name, this.key);
}
module.exports = {
name,
key,
say,
};
console.log(exports === module.exports);
console.log(exports);
console.log(module.exports);
上述代码输出结果为
false
{}
{ name: 'aa', key: 10, say: [Function: say] }
两种模式在导出时,其实起作用的依旧是module模块,由于赋值语句是在页面头部,当exports改变指向后,将不起作用。具体原因,待深入研究,做个标记。