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)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。