欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Node.js?中的?module.exports?與?exports區(qū)別介紹

 更新時(shí)間:2022年09月13日 14:34:52   作者:Chrislinlin  
這篇文章主要介紹了Node.js中的module.exports與exports區(qū)別介紹,每個(gè)模塊中都有module對(duì)象,存放了當(dāng)前模塊相關(guān)的信息,更多相關(guān)內(nèi)容需要的朋友可以參考一下

介紹

  • 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

從源碼中理解

github.com/nodejs/node…

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編碼規(guī)范

    Node.js編碼規(guī)范

    編碼規(guī)范比比皆是,但是很少有公司做好編碼規(guī)范的。忍不住想談?wù)劸幋a規(guī)范的重要性,希望所有人都能夠重視起來(lái)。
    2014-07-07
  • Node.js中readline模塊實(shí)現(xiàn)終端輸入

    Node.js中readline模塊實(shí)現(xiàn)終端輸入

    本文主要介紹了Node.js中readline模塊實(shí)現(xiàn)終端輸入,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 深入理解NodeJS 多進(jìn)程和集群

    深入理解NodeJS 多進(jìn)程和集群

    這篇文章主要介紹了深入理解NodeJS 多進(jìn)程和集群,詳細(xì)的介紹了什么是進(jìn)程和進(jìn)程的實(shí)現(xiàn)等,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-10-10
  • NodeJS如何實(shí)現(xiàn)同步的方法示例

    NodeJS如何實(shí)現(xiàn)同步的方法示例

    nodeJS最大的特點(diǎn)就是其異步操作,下面這篇文章主要給大家介紹了關(guān)于NodeJS如何實(shí)現(xiàn)同步的方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • nodejs修復(fù)ipa處理過(guò)的png圖片

    nodejs修復(fù)ipa處理過(guò)的png圖片

    ipa本身是一個(gè)zip文件改后綴后解壓縮就能看到應(yīng)用內(nèi)使用的資源文件,其中png圖片資源xcode打包的時(shí)候做了些手腳下面我們來(lái)看看如何修復(fù)這些問(wèn)題
    2016-02-02
  • 基于promise.js實(shí)現(xiàn)nodejs的promises庫(kù)

    基于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實(shí)現(xiàn)生成文件并在前端下載

    nodejs實(shí)現(xiàn)生成文件并在前端下載

    這篇文章主要介紹了nodejs實(shí)現(xiàn)生成文件并在前端下載,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 使用Nodejs編寫一個(gè)腳本實(shí)現(xiàn)markdown轉(zhuǎn)pdf功能

    使用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-05
  • node 標(biāo)準(zhǔn)輸入流和輸出流代碼實(shí)例

    node 標(biāo)準(zhǔn)輸入流和輸出流代碼實(shí)例

    這篇文章主要介紹了node 標(biāo)準(zhǔn)輸入流和輸出流代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • nodejs實(shí)現(xiàn)聊天機(jī)器人功能

    nodejs實(shí)現(xiàn)聊天機(jī)器人功能

    這篇文章主要介紹了nodejs實(shí)現(xiàn)聊天機(jī)器人功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論