Node.js - module.exports 與 exports 的區(qū)別

簡而言之 exports是 module.exports本地文件中的快捷方式(相當(dāng)于引用)棚瘟,它不能很好的實(shí)現(xiàn)導(dǎo)出功能嗜憔。

a.js

exports.f = 123123

b.js

const a = require('./a');
console.log(a.f);

這樣可以正常從a導(dǎo)出,在b.js中引用
a.js中改為exports = {f: 123}無法導(dǎo)出
a.js中改為module.exports = {f: 123}正常導(dǎo)出

以下為官方文檔翻譯:

module.exports#

Added in: v0.1.16

  • <Object>
    module.exports對(duì)象是由Module系統(tǒng)創(chuàng)建的,有時(shí)這是不能被接受的怜奖;很多人希望他們的module是一些class的實(shí)例。為此翅阵,將期望導(dǎo)出的對(duì)象賦值到module.exports歪玲。需要注意的是賦值期望的對(duì)象到exports會(huì)簡單的將本地exports變量重新綁定,這很可能并不是所期望的結(jié)果掷匠。

The module.exportsobject is created by the Module system. Sometimes this is not acceptable; many want their module to be an instance of some class. To do this, assign the desired export object to module.exports. Note that assigning the desired object to exports will simply rebind the local exports variable, which is probably not what is desired.

舉個(gè)例子滥崩,假設(shè)我們建了一個(gè)叫a.js的模塊
For example suppose we were making a module called a.js

const EventEmitter = require('events');

module.exports = new EventEmitter();

// Do some work, and after some time emit
// the 'ready' event from the module itself.
// 搞點(diǎn)事情,過一秒之后將'ready'事件從模塊自身提交出去
setTimeout(() => {
  module.exports.emit('ready');
}, 1000);

在另一個(gè)文件中我們可以這么做
Then in another file we could do

const a = require('./a');
a.on('ready', () => {
  console.log('module a is ready');
});

要注意給module.exports賦值必須立即執(zhí)行讹语,而不能在回調(diào)中進(jìn)行钙皮,以下為錯(cuò)誤示例:

Note that assignment to module.exports must be done immediately. It cannot be done in any callbacks. This does not work:

x.js:

setTimeout(() => {
  module.exports = { a: 'hello' };
}, 0);

y.js:

const x = require('./x');
console.log(x.a);

exports shortcut#

Added in: v0.1.16
exports變量在模塊的文件級(jí)范圍內(nèi)是有效的,并且會(huì)在模塊評(píng)估之前被賦值為module.exports的值

它允許有一個(gè)快捷鍵顽决,所以module.exports.f = ...可以更簡潔的寫為exports.f = ...然而短条,要意識(shí)到,和其他變量一樣才菠,假如有一個(gè)新的值被賦到exports上茸时,那么它將不再綁定到module.exports:

The exportsvariable is available within a module's file-level scope, and is assigned the value of module.exports before the module is evaluated.

It allows a shortcut, so that module.exports.f = ... can be written more succinctly as exports.f = .... However, be aware that like any variable, if a new value is assigned to exports, it is no longer bound to module.exports:

module.exports.hello = true; // Exported from require of module 從需求模塊中導(dǎo)出
exports = { hello: false };  // Not exported, only available in the module 沒有導(dǎo)出,僅在模塊中有效

當(dāng)module.exports屬性被完全替換為一個(gè)新的對(duì)象時(shí)赋访,通常也會(huì)重新賦值exports可都,例如:
When the module.exports property is being completely replaced by a new object, it is common to also reassign exports, for example:

module.exports = exports = function Constructor() {
  // ... etc.
};

為了闡明這個(gè)行為缓待,猜想一下require()的假想實(shí)現(xiàn)(比實(shí)際簡單很多):
To illustrate the behavior, imagine this hypothetical implementation of require(), which is quite similar to what is actually done by require():

function require(/* ... */) {
  const module = { exports: {} };
  ((module, exports) => {
    // Module code here. In this example, define a function.
    function someFunc() {}
    exports = someFunc;
    // At this point, exports is no longer a shortcut to module.exports, and
    // this module will still export an empty default object.
    module.exports = someFunc;
    // At this point, the module will now export someFunc, instead of the
    // default object.
  })(module, module.exports);
  return module.exports;
}

原文鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市渠牲,隨后出現(xiàn)的幾起案子旋炒,更是在濱河造成了極大的恐慌,老刑警劉巖签杈,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件国葬,死亡現(xiàn)場離奇詭異,居然都是意外死亡芹壕,警方通過查閱死者的電腦和手機(jī)汇四,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來踢涌,“玉大人通孽,你說我怎么就攤上這事≌霰冢” “怎么了背苦?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長潘明。 經(jīng)常有香客問我行剂,道長,這世上最難降的妖魔是什么钳降? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任厚宰,我火速辦了婚禮,結(jié)果婚禮上遂填,老公的妹妹穿的比我還像新娘铲觉。我一直安慰自己,他們只是感情好吓坚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布撵幽。 她就那樣靜靜地躺著,像睡著了一般礁击。 火紅的嫁衣襯著肌膚如雪盐杂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天哆窿,我揣著相機(jī)與錄音链烈,去河邊找鬼。 笑死更耻,一個(gè)胖子當(dāng)著我的面吹牛测垛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播秧均,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼食侮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了目胡?” 一聲冷哼從身側(cè)響起锯七,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎誉己,沒想到半個(gè)月后眉尸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡巨双,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年噪猾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片筑累。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡袱蜡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出慢宗,到底是詐尸還是另有隱情坪蚁,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布镜沽,位于F島的核電站敏晤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏缅茉。R本人自食惡果不足惜嘴脾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蔬墩。 院中可真熱鬧统阿,春花似錦、人聲如沸筹我。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蔬蕊。三九已至结澄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間岸夯,已是汗流浹背麻献。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留猜扮,地道東北人勉吻。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像旅赢,于是被迫代替她去往敵國和親齿桃。 傳聞我的和親對(duì)象是個(gè)殘疾皇子惑惶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 今天被攜程....幼師...打孩子.....喂芥末......安眠藥....這些關(guān)鍵詞霸屏了香到。 微博鱼冀,公眾號(hào),頭條...
    菲凡說閱讀 628評(píng)論 0 49
  • 總有人錯(cuò)過一場梅雨 迷路在四月的柳絮 隔著千里暮靄找尋 天地不懂 總有人拜了一路的廟宇 只求一場精打細(xì)算的相遇 縱...
    胡子不歸閱讀 506評(píng)論 0 6
  • 滴滴的雨悠就,微微的風(fēng)千绪,開著小窗,宿舍四處涼爽梗脾。 三人已睡荸型,呼聲四起,伴隨著風(fēng)藐唠,參雜著雨帆疟,我卻毫無睡意。 抬起手機(jī)宇立,簡...
    古德曼here閱讀 223評(píng)論 0 2
  • GCC: GNU Compiler CollectionGCC屬于傳統(tǒng)編譯器踪宠,傳統(tǒng)編譯器的工作原理基本上都是三段...
    IRONYT閱讀 4,211評(píng)論 0 1
  • 《郭靖黃蓉》 英雄少年思家國 蓉女佳人最婀娜 桃花島上雙雕落 星云夢里羨黃郭 《七夕》 星云河漢雨細(xì)細(xì) 牽牛織女影...
    人間正道是滄桑lq閱讀 219評(píng)論 0 0