JSDC.tw
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: jsdc2021 --- # #Session 2 JavaScript 模組進化論 - 模組化的演進與實戰 {%hackmd 2YoCBdBPTjmHXDr9lEpWuQ %} > 從這開始 > 可以直接打字做共筆囉 :point_down: Mujing - JavaScript 概念三明治 ![](https://i.imgur.com/pbH0Ltf.png) <!-- 居然是從 Session 2 開始 --> ## Outline ![](https://i.imgur.com/3M91xnb.png) ## 模組化概念 - 功能拆分 - 舉例 - Google 的模組化手機 - 已經取消 - 筆電 - RAM - 各種不同零件 - 密不可分的功能 -> 可自由組裝的組件 - 組合後還是可以照常運作 ## 為什麼 JavaScript 需要模組 ![](https://i.imgur.com/5jo6C7S.png) - 所有 code 共用同一個 scope - 容易造成意外 overwrite - script 標籤引入檔案的順序很重要 - jQuery 後才能用 - [IIFE](https://developer.mozilla.org/zh-TW/docs/Glossary/IIFE) 的誕生 - `(function () {})()` - 建立 scope - 與 global 作區別 - 不過沒有解決 script 需要按照順序載入的問題 - 舉例 - ![](https://i.imgur.com/XOYNUcW.png) <!-- Bundle 後的很常看到 --> <!-- Webpack 甚至可以看到一堆 switch --> ## 模組系統規範 Module Specification - ![](https://i.imgur.com/6qNaq66.png) - 模組與模組之間需要互相溝通 - Module <-> Module - 需要有人居中協調 - 用法必須一致 - 訂定模組規範 - 模組系統 - - AMD - UMD ![](https://i.imgur.com/PF8q7dD.png) ### CommonJS ![](https://i.imgur.com/k6nCe0g.png) - Advantage - 使用 require 引入其他模組 - 使用 module.exports 輸出模組 - 不用擔心自己的程式碼被覆寫掉(?) - Disadvantage - CommonJS 不適合瀏覽器 - 主要為伺服器端設計 - synchronous - 確保相依模組執行時已經載入 - 不需要像瀏覽器下載遠端的 js 檔 - Common 非彼 Common ### 瀏覽器上的 JavaScript 需要⋯⋯ ![](https://i.imgur.com/3aG8Ugl.png) ### AMD ![](https://i.imgur.com/Tqsrfrr.png) - 非同步 - `define()` - 用 return 的內容決定內容 - 用 `require([], callback)` 進行載入 - Callback 地獄! <!-- 類似 AngularJS --> #### on Browsers ##### 設定進入點 ![](https://i.imgur.com/QnDa5j7.png) - `data-main` 是主檔案 - `src` 是 RequireJS Loader - 模組系統規範的實作 - 主要用在瀏覽器 ### UMD ![](https://i.imgur.com/cPznW7z.png) <!-- 常見於 Bundle 後的檔案 ...--> - 同一份程式碼可以在 AMD 和 CommonJS 下使用 - 瀏覽器和 Server-side 都能用 - UMD 最外層是 IIFE - 判斷並根據不同模組系統進行不同的動作 - `(function (global, factory) {})(this, 模組內容)` ## ESM (Modern) ![](https://i.imgur.com/5IaZAHG.png) ![](https://i.imgur.com/TZMRctu.png) - ECMAScript Module - 以前需要用 Bundler 轉成 ES5 支援的規範 - 現代瀏覽器普及後或許不再需要 - 瀏覽器內要加 type="module" ### Stage 1. 載入檔案 (Construction) - ![](https://i.imgur.com/8V8bng1.png) - 下載到瀏覽器端 - 紀錄 export / import 之間的關係資訊 2. 空間連結配置 (Instantiation) - ![](https://i.imgur.com/FNMhP2S.png) 3. 變數賦值 (Evaluation) - ![](https://i.imgur.com/1K06yHG.png) - 可以分開執行 - 為瀏覽器行為訂製 ### Conclusion ![](https://i.imgur.com/fcEvHJj.png) 解析方式: <!-- ES Modules 的 import() 是不是靜態解析存疑 ---> - ES Modules 會先解析出相依關係,沒使用的剃掉 #### Advantage - 明確指定模組名稱 - 非同步載入 - 真正執行之前可以先被解析,用於 Tree Shaking 優化 - 只留需要的元件,不需要的甩掉 - 常見的 bundler 都有這種優化 - 歸功於 ESM 的功勞 ![](https://i.imgur.com/KDkS3ix.png) #### Disadvantage <!-- 沒缺點¿ --> <!-- pan 補充 --> - ESM 不方便動態載入 - 比如探索整個資料夾之後自動載入資料夾中的模組 - CommonJS 在 Node.js 上比較方便 - ESM 有往 NJS 推,但至今還不算主流 - 需要加 Flag 開啟 “實驗功能” - 要用 ESM 建議搭配 TypeScript - 可以 compile to CommonJS ## 參考 ![](https://i.imgur.com/jR3ObOu.png) 範例程式碼: https://github.com/moojing/javascript-module-evlution 實力三明治 https://www.facebook.com/powersandwich

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully