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中readline模塊實(shí)現(xiàn)終端輸入
本文主要介紹了Node.js中readline模塊實(shí)現(xiàn)終端輸入,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02基于promise.js實(shí)現(xiàn)nodejs的promises庫(kù)
promise是JavaScript實(shí)現(xiàn)優(yōu)雅編程的一個(gè)非常不錯(cuò)的輕量級(jí)框架。該框架可以讓你從雜亂的多重異步回調(diào)代碼中解脫出來(lái),并把精力集中到你的業(yè)務(wù)邏輯上。2014-07-07使用Nodejs編寫一個(gè)腳本實(shí)現(xiàn)markdown轉(zhuǎn)pdf功能
Markdown?是一種輕量級(jí)的標(biāo)記語(yǔ)言,非常適合用來(lái)寫作和記錄,將?Markdown?轉(zhuǎn)換為?PDF?可以讓文檔在格式和樣式上更加統(tǒng)一,也方便在不同設(shè)備和平臺(tái)上查看和打印,在接下來(lái)的內(nèi)容中我們將講解如何使用?NodeJs?編寫一個(gè)?Markdown?轉(zhuǎn)?PDF?的腳本來(lái)實(shí)現(xiàn)我們這個(gè)想要的功能2024-05-05node 標(biāo)準(zhǔn)輸入流和輸出流代碼實(shí)例
這篇文章主要介紹了node 標(biāo)準(zhǔn)輸入流和輸出流代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09nodejs實(shí)現(xiàn)聊天機(jī)器人功能
這篇文章主要介紹了nodejs實(shí)現(xiàn)聊天機(jī)器人功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09