15T
      • 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
    • 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 Versions and GitHub Sync Note Insights 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
    2
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Constructor Function Constructor? # 函式建構器(Function Constructor) function constructor 的功能主要在於建立一個function,方法如下: ```javascript= const multiply = new Function("x", "y", "return x * y"); ``` 這段程式碼意思是建立一個function multiply包含x, y兩個參數,回傳x + y ![image](https://hackmd.io/_uploads/BJ-u1cd-0.png) ![image](https://hackmd.io/_uploads/SyiRCDPbC.png) 函式建構器是動態生成function的方法,也相較於eval()解決環境污染問題,由於動態生成,所以可以達到下面的效果: ```javascript= function test(){ return `"'1' + '2'"`; } const test1 = new Function(`return ${test()}`); console.log(test()); console.log(test1()); ``` 先靜態建立function test() 在動態建立test1,回傳值抓取test()的值 ![image](https://hackmd.io/_uploads/ByqcrK_ZC.png) test()回傳的是引號``字串"'1' + '2'" 而test1()回傳引號""內的字串'1' + '2' 有看出不一樣嗎?靜態單純回傳所有內容,動態會辨識字串裡的內容 還是不懂?把最外面引號``拿掉看看 ```javascript= function test(){ return "'1' + '2'"; } ``` ![image](https://hackmd.io/_uploads/ryq3UFuZA.png) test()回傳字串引號內的字串'1' + '2' test1()回傳字串12,也就是字串'1'跟'2'的相加 靜態的test()回傳雙引號""裡面的內容(字串) 動態的test1()則是執行引號""裡面的內容並回傳 現在改成: ```javascript= function test(){ return "4 * 6" } ``` ![image](https://hackmd.io/_uploads/r1NWkquW0.png) 結果就是靜態的test()回傳字串4 * 6 動態test1()回傳引號內的運算結果 [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function) # 建構器函式(Constructor Function) 通常是用創造物件的工具,可以快速產生結構類似的物件 使用方法如下: 定義一個建構函式Constructor Function ```javascript= function MBTI(name, MBTI, gender){ this.name = name; this.MBTI = MBTI; this.gender = gender; } ``` 這邊我做了一個可以創造有**名字**、**十六型人格**跟**性別**的物件 接者把它拿來用! ```javascript= let Jeremy = new MBTI("Jeremy", "INFJ", "male"); let Watson = new MBTI("Watson", "ENFJ", "male"); let Fang = new MBTI("Fang", "ISFJ", "female"); let Jami = new MBTI("Jami", "INFP", "female"); let Tangerine = new MBTI("Tangerine", "ISFJ", "female"); ``` 如此一來我就建立了五個新的物件,裡面包含我們的十六行人格 拿三個出來看看: ```javascript= console.log(Jeremy); console.log(Fang); console.log(Tangerine); ``` ![image](https://hackmd.io/_uploads/BkFdZjuZC.png) 確實有這三個MBTI的物件~ [參考資料](https://javascript.alphacamp.co/constructor-function.html) # 建構函式與prototype的應用 這邊我想把每個物件裡的內容組成句子,函式如下: ```javascript= const title = (this.gender == "male") ? "his" : "her"; return `${this.name}, ${title} MBTI is ${this.MBTI}.` ``` 直覺上會想把他也加進建構函式裡面: ```javascript= function MBTI(name, MBTI, gender){ this.name = name; this.MBTI = MBTI; this.gender = gender; const title = (this.gender == "male") ? "his" : "her"; this.describe = function(){ return `${this.name}, ${title} MBTI is ${this.MBTI}.` } } ``` 執行出來會長這樣: ![image](https://hackmd.io/_uploads/r1Djf6_bC.png) 執行裡面的function describe: ![image](https://hackmd.io/_uploads/B1PZQaub0.png) 可以得到我想要的: ![image](https://hackmd.io/_uploads/H10mXpuWA.png) 此時會有佔據過多記憶體的問題,在我建構每個MBTI時,都會建立describe這個function,有1000個物件就存1000個function,這時候可以思考其實每個function都長一樣,可以共用,prototype這個屬性就幫我們實現了~ 在MBTI的prototype裡面加上一個function describe: ```javascript= MBTI.prototype.describe = function(){ const title = (this.gender == "male") ? "his" : "her"; return `${this.name}, ${title} MBTI is ${this.MBTI}.` } ``` 此時describe這個function就是所有MBTI物件的共用方法 Chris說現在寫法是這樣: :100: ```javascript= class MBTI { constructor(name, MBTI, gender){ this.name = name; this.MBTI = MBTI; this.gender = gender; } describe (){ const title = (this.gender == "male") ? "his" : "her"; return `${this.name}, ${title} MBTI is ${this.MBTI}.` } } ``` ~~上面舊的寫法不直覺不好記~~ :heavy_check_mark: 並沒有新舊,上面是原本寫法,下面是簡寫,寫可以寫下面加速開發,但理解要理解上面。

    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