exports和module.exports的區(qū)別
- 1控轿、
module.exports
初始值是個(gè)空對(duì)象{}
腿时,exports
一開(kāi)始是指向module.exports
的傀缩,所以exports
初始值也是個(gè)空對(duì)象{}
后德;
- 舉例:
module
和exports
這兩個(gè)對(duì)象是Node.js
的文件中默認(rèn)隱式存在
console.log(exports);
console.log(module);
//在終端運(yùn)行:
{}
Module {
id: '.',
exports: {},
...
}
- 2硝全、通過(guò)
require
得到的是module.exports
中的內(nèi)容栖雾,而不是exports
的內(nèi)容。即如果module.exports
當(dāng)前沒(méi)有任何屬性的話伟众,exports
會(huì)把屬性賦給module.exports
析藕,如果module.exports
中已經(jīng)存在一些屬性的話,exports
中的屬性會(huì)被忽略
- 舉例:
//把下面的內(nèi)容放到a.js:
exports.add = 123;
module.exports={
hello:1234
}
//然后在另一個(gè)b.js文件中執(zhí)行它:
var rocker = require('./a.js');
console.log(rocker.add); //undefined
console.log(rocker.hello); //1234
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者