隨寫筆記
      • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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
    • Make a copy
    • 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 Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    Scope === 規範變數在程式碼中的取用規則,Scope 主要分成兩種類型: - Lexical Scope ( 語彙範疇 ) - Dynamic Scope ( 動態範疇 ) JavaScript 與大多數語言採用 Lexiacal Scope。 Lexical Scope (語彙範疇) --- 代表區塊間的包裹關係,被包裹在內層的區塊可以保護自己的變數不被外層取用,相反的,外層區塊的變數仍能被內層區塊使用。 ```jsx let outer = '此處是外層'; function innerFun() { var inner = '此處是內層'; console.log(outer); // "此處是外層" console.log(inner); // "此處是內層" } console.log(outer); // "此處是外層" console.log(inner); // Uncaught ReferenceError: inner is not defined ``` - 函式參數屬於內層 Scope,只有在函式內能夠使用 ![](https://i.imgur.com/PIXZ7Gu.png) - 巢狀 Scope,規則相同:外層 Scope 無法取用內層變數,但內層 Scope 可以取用外層變數 ![](https://i.imgur.com/BCn2afh.png) 全域作用域 Global Level Scope --- 每個執行 JavaScript 的環境,都有一個全域物件(Global Object): - 在 HTML 裡,全域物件是 `window` object。 - 在 Node.js 裡,全域物件是 `global` object。 存放在全域物件裡的變數,無論在哪裡宣告,效力遍及整個程式。因此不應該隨便產生不需要的全域變數,造成額外的程式風險,以及變數控管上的困擾。 使用 var 才會是 Global Level Scope,若使用 let、const 不全然是 Global Level Scope。詳細見[此](https://hackmd.io/9jqUIElFRcavjEriaCBDCg?view#%E4%B8%8D%E6%9C%83%E7%94%A2%E7%94%9F-Global-Scope-%E8%AE%8A%E6%95%B8)。 ### 賦值給未宣告的變數 賦值給未宣告的變數,自動產生全域變數,一般應該避免這種寫法。 ```jsx function myFunc(){ n1 = "OneJar"; // 賦值給未宣告的變數,自動產生全域變數 console.log(n1); // OneJar console.log(this.n1); // OneJar console.log(window.n1); // OneJar } myFunc(); console.log(n1); // OneJar ``` 函式作用域 Function Level Scope --- > 同時也是 Block Level Scope,只是為了方便區分,才特此分出來。 只有在限定範圍內有效果,形成 Scope 的基本單位: - Function JavaScipt 不是為每個 `{}` 產生 Scope,因此 `if() {}`、`for() {}` 內層的變數仍能被外層取用。 區塊作用域 Block Level Scope --- 只有在限定範圍內有效果,形成 Scope 的基本單位: - try/catch 中的 `catch`、`with` - ES6:`let`、`const` ### ES6:`let`、`const` 在 ES5 以前,用 var 關鍵字宣告的變數,只會有 Global Level 和 Function Level 兩種等級的作用域,因此`{}` 不會產生 Scope,只有 Function 才會。 ES6 導入新的變數宣告關鍵字:let 和 const,增加了 Block Scope 的用途。使用 let、const 進行變數宣告,將會在 `{}` 產生 Scope。 ```jsx { let x = 2; { let x = 10; console.log(x); // 10 } console.log(x); // 2 } ``` > [詳細的變數運用](https://hackmd.io/9jqUIElFRcavjEriaCBDCg?view) Scope Lookup(查找) --- 由當下的 Scope 往外層 Scope 查找,一旦找到就停止。若是一直沒有找到變數,JavaScript 就會由當下的 Scope 一路查找到最外層的 Global Scope。 此規則下,可能衍生的兩種問題: - ### 直到 Global Scope 都沒有找到該變數 再細分情境,查找這個變數,是為了要「取值」or「賦值」。 - 在取值情境,會發出 Reference Error:`Uncaught ReferenceError: XXX is not defined`。 - 在賦值情境,會新建變數 or 發出錯誤: - 通常情況,JavaScript 會一路查找到 Global Scope,發現連 Global Scope 都沒有時,便會建立新的 Global 屬性。 - 然而在 `'use strict'`([JavaScript 的嚴格模式](https://hackmd.io/FZI5Jq1SRBWwJGWMqN-cHA?view))時,在賦值時便會報錯。在 `'use strict'`,JavaScript 不接受對不存在變數賦值。 ```jsx 'use strict'; function assignToNonExist() { notDeclaredVar = 'I am is not Declared';  // Uncaught ReferenceError: notDeclaredVar is not defined } assignToNonExist(); ``` - ### Shadowing (遮蔽) 如果同時有兩個同名變數宣告在不同層的 Scope,內層 Scope 的變數會 Shadow(遮蔽)外層的同名變數。 ![](https://i.imgur.com/aAEYMm8.png) 由內層 Scope 往外層 Scope 找,馬上找到自己的 Scope 內有一個 myString,於是停止查找,而外層的 myString 理所當然被 Shadow(遮蔽)。 ```jsx var myString = "hello global"; function testShadowing(){ var myString = "hello scope"; console.log(myString); // "hello global" console.log(window.myString); // "hello global" } ``` 然而,如果在內層透過如 `window.n1` 的方式,明確表明「我要取的是 Global 變數的 n1,而不是 Local 的 n1」,就會是 Global 變數發揮效用。 實作建議 --- - 雖然有 Shadowing 這個特性,仍盡量不要重複宣告同名變數。 - 宣告同名變數,會造成語意不清,難以維護。 - 屬性存取是藉由屬性存取規則(Prototype)去查找的,並不是 Scope 的工作。 - 可以使用 [eval](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) 脫離 Lexical Scope,但是盡量別用。 - eval 可以輸入字串,編譯器會在執行時期將它轉成程式碼,也就是脫離 Lexical Scope 在編譯時期就定好 Scope 的概念。 - 使用 eval 程式碼難以 Debug,也會造成效能低落,因為編譯器無法預先知道執行的程式是什麼,無法最佳化 eval 述句與相關程式碼的效能。

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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