JavaScript 模組

aow054發表於2024-09-20
現在我們不再將所有 js 寫在一個檔案中併傳送給客戶端。今天,我們將程式碼編寫到模組中,這些模組之間共享資料並且更易於維護。約定是使用駝峰命名法命名模組。我們甚至可以透過 npm 儲存庫將第 3 方模組包含到我們自己的程式碼中,例如 jquery、react、webpack、babel 等。最終的捆綁包是由部署到產品伺服器的小模組檔案構建的,最終傳送給客戶端。舊版瀏覽器不支援模組出於效能原因,最好將小js檔案傳送到瀏覽器。模組是 js 中非常重要的一部分,開發人員已經在其他語言中使用了數十年。模組:可重用的一段程式碼,封裝了專案某個部分的實現細節。它是一個獨立檔案,但不一定是。模組也可以有匯入和匯出原生 es6 模組:es6 之前沒有模組。必須自己實現或使用外部庫。模組儲存在檔案中,每個檔案只有一個模組。我們可以從模組中匯出值、函式等。我們匯出的任何內容都稱為公共 api,公開供其他程式碼使用。這個公共 api 透過將公共程式碼匯入到模組中來使用,稱為依賴項。簡單的應用程式也可以在沒有模組的情況下編寫,但是對於企業規模的專案,我們需要模組。高階:-> 使編寫軟體變得非常容易,因為模組是我們組合在一起構建複雜應用程式的小構建塊。-> 隔離元件:每個功能都可以完全隔離地開發,無需擔心其他開發人員的工作或整個系統的工作方式。-> 程式碼抽象:在模組中實現低階程式碼,將這些抽象匯入到其他模組中,自然會導致更有組織的程式碼庫。-> 程式碼可重用性:允許我們在多個專案中重用程式碼。現代 js 使用捆綁和轉譯。## bundling = is a complex process which involves:a. eliminate unused codeb. combine all modules into single file.c. compress the code.ex. webpack build tool - requires manual config, hence complex.ex. parcel - zero build tool as we don't need to write any setup code.## transpiling/polyfiling:convert modern js to es5 or older syntax such that older browser supports the application. ex. done using tool called babelentire process results in the final bundle file for production ready to be deployed on server. 登入後複製### scripts are also files but they are different from es6 modules in the following ways:script[used earlier]:- all top level variables are always global. this leads to global namespace pollution and name-coliision.- default mode: sloppy mode- top level 'this' points to window object.- import/export of values is not allowed.- linked to html using plain script tag.- downloaded by default in sync way, unless async or defer attribute is used.### es6 modules:- all top-level variables are scoped to module i.e they are private by default. such a value can be access by outside varible only when its exported by the module else it will be inaccessible for outside world.- default mode: strict mode. hence with modules no more need to declare strict mode.- top level 'this' is 'undefined'- allows import/export of values using es6 syntax of import/export.- link to html file using <script type="module"> tag.- file download always happens in an async way, i.e for module loaded from html or an import one module to another.</script>登入後複製## Understanding module import process:Parsing -&gt; [Asyn module download, Linking import-export, Execute individual module itself] -&gt; Execution overall index.js- import/export only need to happen at the top level of if-block or any function. Imports are hoisted. Importing values is always the first thing which happens in a module.- Top-level static imports make imports known before execution. - Modules are loaded in async way from the server, importing happens in sync manner. - Parsing refers to reading the code without executing it in which imports are hoisted. Modules are imported even before execution. Hence, it enables bundling &amp; code elimation even before fn exection. [V Impt as large projects have 100s of modules, and 3rd party modules also from which we want the small piece and not the entire module]- Bundlers can then join multiple modules, and then eliminate code. hence, we can easily import/export from code.- After a module arrives, its parsed and then the module exports are linked to imports in index.js i.e live connection is established between import inside index.js and export statement of the module file. They are not copied. Import is just a reference to the exported value. Hence, when the value changes in the exporting module, it also changes in the importing module too. This concept is unique to ES6 modules, other module system don't work like this.- Code in the imported modules is executed.登入後複製 以上就是JavaScript 模組的詳細內容,更多請關注我的其它相關文章!

相關文章