nodejs模組化的好處:1、程式碼解耦,可以提高程式碼的複用性;2、可以提高程式碼的可維護性;3、可以提高程式碼的可讀性;4、可以實現按需載入;5、可以解決命名衝突;6、方便管理依賴。
本教學操作環境:windows7系統、nodejs 12.19.0版,DELL G3電腦。
什麼是模組化?
在程式設計領域中,模組化就是遵守固定的規則,把一個大檔案拆分成獨立並且相互依賴的多個小模組.通俗來講就是一個js檔案可以使用另一個js檔案中的資料.
模組化有什麼好處?
1、程式碼解耦,可以提高程式碼的複用性
2、可以提高程式碼的可維護性
3、可以提高程式碼的可讀性
4、可以實作按需載入
5、可以解決命名衝突
6、方便管理依賴
模組化的產生也帶來了模組作用域,同函數作用域相似,在自訂模組中定義的變數、方法等,只能在目前作用域內被訪問,這種訪問限制,有效的解決了變數污染的問題。
模組規範
當模組化規範就是對程式碼進行模組化的分割與組合時,需要遵守的那些規則,例如:
使用什麼樣的語法格式來引用模組
#在模組中使用什麼樣的語法格式向外揭露成員
#模組化規範的好處:大家都遵守同樣的模組化規範寫程式碼,降低了溝通的成本,極大方便了各個模組之間的相互調用,利人利己
模組規格種類:
CommonJS 規格:nodejs中遵守的就是commonjs規格。
ES6 模組化規格:(前後端通用的模組化規格;Node.js、Vue、React 中都能使用!)
CMD 與AMD 模組化規格(較少使用): CMD--sea.js, AMD-require.js
-
UMD 叫做通用模組定義規範(Universal Module Definition),它可以透過執行時間或編譯時讓同一個程式碼模組在使用CommonJs、CMD 甚至是AMD 的專案中運作。它沒有自己專有的規範,是集結了 CommonJs、CMD、AMD 的規範於一身。
區別:
1. 對於依賴的模組,AMD 是提前執行,CMD 是延遲執行。
2. CMD 推崇依賴就近,AMD 推崇依賴前置。
nodejs模組分類
- #內建模組--- fs(檔案系統模組).path(路徑模組).http(網路操作模組)...
const fs = require("fs"); //node提供的基础模块,可以直接导入使用
- 自訂模組--- 使用者自己建立的.js檔案
const m = require("./js文件名");//注意其中是相对路径,后缀名.js可加可不加
- 第三方模組--- 由第三方個人或團隊開發出來的模組,通常叫做套件,使用前需要下載導入
const moment = require('moment');// 使用前需要通过npm i moment 命令安装该包
模組的導入導出
導入
使用require()方法載入模組
注意:使用require方法載入其他模組時,會執行被載入模組中的程式碼
導出
- 在每個自訂模組中都有一個module物件,它裡面儲存了和目前模組有關的資訊,其中一個屬性exports可以將模組成員暴露,供外界使用.載入某個模組時得到的就是module.exports這個屬性指向的一個空物件.
- 若想使用該模組中的資料,可以在module.exports這個屬性中加入屬性值供外界使用
- 為了簡化向外共享成員的程式碼,node提供了exports物件.預設,exports和module. exports指向同一個物件.
#注意: 使用require()方法導入模組時,導入的結果永遠以module.exports指向的物件為準
#exports和module.exports的使用誤區
exports = { username: 'ls', gender: '男' } module.exports = exports; module.exports.age = '20'; // 导入后的结果为 {username:'ls',gender:'男',age:'20'} exports = { username: 'ls', gender: '男' } module.exports.age = '20'; module.exports = exports; // 导入后的结果为 {username:'ls',gender:'男'} exports = { username: 'ls', gender: '男' } exports = module.exports; module.exports.age = '20'; // 导入后的结果为 {age:'20'}
所以使用時盡量只使用其中一個方法,避免造成不必要的麻煩
更多node相關知識,請造訪:nodejs 教學! !
以上是nodejs模組化有什麼好處的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Include:1)AsteeplearningCurvedUetoItsVasteCosystem,2)SeochallengesWithClient-SiderEndering,3)潛在的PersperformanceissuesInsuesInlArgeApplications,4)ComplexStateStateManagementAsappsgrow和5)TheneedtokeEedtokeEedtokeEppwithitsrapideDrapidevoltolution.thereedtokeEppectortorservolution.thereedthersrapidevolution.ththesefactorsshesssheou

reactischallengingforbeginnersduetoitssteplearningcurveandparadigmshifttocoment oparchitecent.1)startwithofficialdocumentationforasolidFoundation.2)了解jsxandhowtoembedjavascriptwithinit.3)

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

javascriptfatigueinrectismanagbaiblewithstrategiesLike just just in-timelearninganning and CuratedInformationsources.1)學習whatyouneedwhenyouneedit

tateractComponents通過theusestatehook,使用jestandReaCtTestingLibraryToSigulationsimintionsandIntractions and verifyStateChangesInTheUI.1)underthecomponentAndComponentAndComponentAndConconentAndCheckInitialState.2)模擬useruseruserusertactionslikeclicksorformsorformsormissions.3)

KeysinreactarecrucialforopTimizingPerformanceByingIneFefitedListupDates.1)useKeyStoIndentifyAndTrackListelements.2)避免使用ArrayIndicesasKeystopreventperformansissues.3)ChooSestableIdentifierslikeIdentifierSlikeItem.idtomaintainAinainCommaintOnconMaintOmentStateAteanDimpperperFermerfermperfermerformperfermerformfermerformfermerformfermerment.ChosestopReventPerformissues.3)

ReactKeySareUniqueIdentifiers usedwhenrenderingListstoimprovereConciliation效率。 1)heelPreactrackChangesInListItems,2)使用StableanDuniqueIdentifiersLikeItifiersLikeItemidSisRecumended,3)避免使用ArrayIndicesaskeyindicesaskeystopreventopReventOpReventSissUseSuseSuseWithReRefers和4)

獨特的keysarecrucialinreactforoptimizingRendering和MaintainingComponentStateTegrity.1)useanaturalAlaluniqueIdentifierFromyourDataiFabable.2)ifnonaturalalientedifierexistsistsists,generateauniqueKeyniqueKeyKeyLiquekeyperaliqeyAliqueLiqueAlighatiSaliqueLiberaryLlikikeuuId.3)deversearrayIndiceSaskeyseSecialIndiceSeasseAsialIndiceAseAsialIndiceAsiall


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。