module.exports 與 exports 的區(qū)別解釋

// -------- node.js core --------

var module = {
  exports: {
  }
};

exports = module.exports;

exports 是 module.exports的引用。也就是內(nèi)存中指向同一地址。

exports.name = 'jack'
exports.say = ()=>{
console.log(hi) 
}

上面代碼給exports添加了name 和 say 屬性,此時的操作對象是內(nèi)存中指向的那個地址。

exports = {
  name: 'Bob',
  add: function (a, b) {
    return a + b;
  }
}

上面代碼直接給exprots賦值,不過 exports 是一個引用,直接賦值給它,只是讓這個變量等于另外一個引用。所以不會改變module.exports。

module.exports = {
  name: 'Bob',
  add: function (a, b) {
    return a + b;
  }
}

所以 只有通過 module.exports 才能真正修改到 exports 本身

console.log(module) // { exports: { name: 'Bob', add: [Function] } }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容