Node.js?中的?module.exports?與?exports區(qū)別介紹
介紹
module
:每個(gè)模塊中都有module
對(duì)象,存放了當(dāng)前模塊相關(guān)的信息;module.exports
:模塊導(dǎo)出的內(nèi)容;exports
:默認(rèn)情況下,exports
和module.exports
指向同一個(gè)對(duì)象。
示例
test.js
console.log('module', module) console.log('module.exports', module.exports) console.log('exports', exports) console.log('module.exports === exports', module.exports === exports)
控制臺(tái)執(zhí)行 node test.js
,打印日志如下:
module Module { id: '.', path: 'E:\lin\webpack-learning\src\cjs\demo1', exports: {}, filename: 'E:\lin\webpack-learning\src\cjs\demo1\test.js', loaded: false, children: [], paths: [ 'E:\lin\webpack-learning\src\cjs\demo1\node_modules', 'E:\lin\webpack-learning\src\cjs\node_modules', 'E:\lin\webpack-learning\src\node_modules', 'E:\lin\webpack-learning\node_modules', 'E:\lin\node_modules', 'E:\node_modules' ] } module.exports {} exports {} module.exports === exports true
從源碼中理解
const exports = this.exports; const thisValue = exports; const module = this;
說(shuō)明:exports 是 module.exports 的引用
通過(guò)示例理解
示例一
test1.js
exports.name = 'lin'; module.exports.age = 18; console.log('module.exports', module.exports) console.log('exports', exports) console.log('module.exports === exports', module.exports === exports)
index.js
const test = require('./test1') console.log("test", test);
控制臺(tái)執(zhí)行 node index.js
,打印日志如下:
module.exports { name: 'lin', age: 18 } exports { name: 'lin', age: 18 } module.exports === exports true test { name: 'lin', age: 18 }
畫圖說(shuō)明:
示例二
test2.js
module.exports.name = 'lin' exports = { name: 'myName', age: 6 } console.log('module.exports', module.exports) console.log('exports', exports) console.log('module.exports === exports', module.exports === exports)
index.js 改為引入 test2.js 模塊
const test = require('./test2') console.log("test", test);
控制臺(tái)執(zhí)行 node index.js
,打印日志如下:
module.exports { name: 'lin' } exports { name: 'myName', age: 6 } module.exports === exports false test { name: 'lin' }
畫圖說(shuō)明:
示例三
test3.js
module.exports = { name: 'lin', age: 18 } exports.name = "myName" console.log('module.exports', module.exports) console.log('exports', exports) console.log('module.exports === exports', module.exports === exports)
index.js 改為引入 test3.js 模塊
const test = require('./test3') console.log("test", test);
控制臺(tái)執(zhí)行 node index.js
,打印日志如下:
module.exports { name: 'lin', age: 18 } exports { name: 'myName' } module.exports === exports false test { name: 'lin', age: 18 }
畫圖說(shuō)明:
示例四
test4.js
exports = { name: 'lin', age: 18 } module.exports = exports module.exports.job = 'FE' console.log('module.exports', module.exports) console.log('exports', exports) console.log('module.exports === exports', module.exports === exports)
index.js 改為引入 test4.js 模塊
const test = require('./test4') console.log("test", test);
控制臺(tái)執(zhí)行 node index.js
,打印日志如下:
module.exports { name: 'lin', age: 18, job: 'FE' } exports { name: 'lin', age: 18, job: 'FE' } module.exports === exports true test { name: 'lin', age: 18, job: 'FE' }
畫圖說(shuō)明:
小結(jié)
exports
是module.exports
的引用;- 對(duì)
exports
和module.exports
賦值時(shí)要格外注意,明確模塊導(dǎo)出的值; - 使用
require()
導(dǎo)入模塊 A 時(shí),導(dǎo)入的結(jié)果是模塊 A 中module.exports
指向的值。
到此這篇關(guān)于Node.js 中的 module.exports 與 exports區(qū)別介紹的文章就介紹到這了,更多相關(guān)Node.js exports內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Node.JS發(fā)送http請(qǐng)求批量檢查文件中的網(wǎng)頁(yè)地址、服務(wù)是否有效可用
這篇文章主要介紹了Node.JS發(fā)送http請(qǐng)求批量檢查文件中的網(wǎng)頁(yè)地址、服務(wù)是否有效可用,本文通過(guò)實(shí)例代碼文字說(shuō)明給大家講解的非常詳細(xì),需要的朋友參考下2019-11-11Ubuntu22.04系統(tǒng)下升級(jí)nodejs到v18版本
ubuntu默認(rèn)安裝的nodejs版本比較老,要安裝到最新的,下面這篇文章主要給大家介紹了關(guān)于Ubuntu22.04系統(tǒng)下升級(jí)nodejs到v18版本的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06用C/C++來(lái)實(shí)現(xiàn) Node.js 的模塊(二)
上篇文章的主要內(nèi)容講訴了用C/C++來(lái)實(shí)現(xiàn) Node.js 的模塊,本文更深一步繼續(xù)探討這個(gè)問(wèn)題,有需要的朋友可以參考下2014-09-0920行代碼簡(jiǎn)單實(shí)現(xiàn)koa洋蔥圈模型示例詳解
這篇文章主要為大家介紹了20行代碼簡(jiǎn)單實(shí)現(xiàn)koa洋蔥圈模型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01解決npm?run?serve啟動(dòng)報(bào)錯(cuò)npm?ERR?Missing?script:"serve&q
這篇文章主要給大家介紹了關(guān)于解決npm?run?serve啟動(dòng)報(bào)錯(cuò)npm?ERR?Missing?script:"serve"的相關(guān)資料,這是最近開(kāi)發(fā)中遇到的一個(gè)問(wèn)題,文中通過(guò)圖文將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01Node.js實(shí)現(xiàn)簡(jiǎn)單聊天服務(wù)器
Node.js 是一個(gè)基于Chrome JavaScript運(yùn)行時(shí)建立的一個(gè)平臺(tái), 用來(lái)方便地搭建快速的,易于擴(kuò)展的網(wǎng)絡(luò)應(yīng)用,今天我們來(lái)探討下,如何使用node.js實(shí)現(xiàn)簡(jiǎn)單的聊天服務(wù)器2014-06-06