grayshine
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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
    # <font class="h2">this:call, apply, bind</font> ###### tags: `javascript` <style> .h2 { background: linear-gradient(135deg,#fff,#537479) ; color: #537479; display:block; padding: 6px 5px; border-radius: 4px; } .h3 { background: linear-gradient(180deg,#fff 50%,#c9d5d4) ; color: #537479; display:block; padding: 6px 5px; border-bottom: 3px solid #537479; } .h4 { color: #537479; font-weight:bold; font-size:1.1em; } </style> ![](https://i.imgur.com/Kh32idG.png =500x) call, apply, bind是phototype裡面的方法 <br><br><br><br> ### <font class="h4">「綁定物件this」在不同的情況下是不同的東西,但我們可以自己決定函式的綁定物件是什麼</font> <br> :::info call 就是呼叫函式,不過可透過 call 傳入第一個參數去改變 this,後續的參數當作一般參數 傳入,apply 差別在於後續參數是一個陣列,==bind 大致和 call 相同但會產生新的函式==。 ==call、 apply、bind 都是屬於函式的原型方法(Function.prototype)==。 ::: ```javascript function base() { console.log(this, arguments); } base.call({name:"John"}, 2, 3); //會立即執行 base.apply({name:"Mark"}, [2, 3]); //會立即執行 base.bind({name:"Susan"})(2, 3); //bind會產生新的函式,需另外執行 ``` <br><br><br><br> ### <font class="h3">1. 使用bind重新綁定物件</font> ![](https://i.imgur.com/PBGKFJo.png =350x) - bind不會立刻執行,要去調用它才會執行 - 會return一個新的函式 ```javascript function test(){ console.log(this) } test();//window let test2 = test.bind({x:3}); test2();//{x:3} ``` <br> ### <font class="h3">2. 函式呼叫有apply和call兩種方式</font> 強制指定某個物件作為該function執行時的this <br> ### <font class="h4">➤使用apply呼叫函式</font> ![](https://i.imgur.com/SiJQisB.png =320x) <br> ```javascript function add(n1,n2){ console.log("結果",n1+n2); //7 console.log("綁定物件",this); //window } add(3,4); ``` 基本的函式呼叫,無法自己設定綁定物件 ![](https://i.imgur.com/J4o6nIN.png) <br> ```javascript function add(n1,n2){ console.log("結果",n1+n2); //9 console.log("綁定物件",this); //{y: 4} } add.apply({y:4},[4,5]); ``` ![](https://i.imgur.com/G2SzIWo.png) <br><br><br><br><br><br> ### <font class="h4">➤使用call呼叫函式</font> ![](https://i.imgur.com/subUuXp.png =320x) ```javascript function add(n1,n2){ console.log("結果",n1+n2); //14 console.log("綁定物件",this); //{z:10} } add.call({z:10},10,4); ``` ![](https://i.imgur.com/9f5fmZt.png) <br><br><br><br><br><br><br><br> --- <br> call, apply, bind都會影響函式的呼叫方式 - call是立刻執行,然後將this給替換掉 - apply與call也是類似的觀念,而apply與call不同的是帶入參數會是陣列呈現 - ==bind不會立刻執行,要去調用它才會執行== <br> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } fn(1,2)//window,1,2 ``` 這邊`fn(1,2)`方式是屬於simple call,會指向全域`window` <br> ### <font class="h4">使用call的方式</font> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } fn.call(family, 1, 2)//{myName: '小明家'} 1 2 ``` 使用`call`在執行的時候,第一個參數就是this,所以這裡`family`就會替代掉`console.log(this, para1, para2);`的this 所以原本window就會替換成`family`物件 <br> ### <font class="h4">使用apply的方式</font> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } fn.apply(family,[3,4])//{myName: '小明家'} 3 4 ``` apply與call也是類似的觀念,而apply與call不同的是帶入參數會是陣列呈現 <br> ### <font class="h4">使用bind的方式</font> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } var fn2 = fn.bind(family,'小明','杰倫'); fn2();//{myName: '小明家'} '小明' '杰倫' ``` bind不會立刻執行,因此我們在使用bind時,可以先宣告一個變數`fn2`,之後`fn2()`再去調用它才會執行 <br> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } var fn2 = fn.bind(family,'小明','杰倫'); fn2(1,2);//{myName: '小明家'} '小明' '杰倫' ``` 這裡我們是另外帶入參數`1,2`看看,會發現沒有變化 我們在使用bind時就會先強制把參數寫入,如`小明,杰倫` <br> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } var fn2 = fn.bind(family,'小明'); fn2(1,2);//{myName: '小明家'} '小明' 1 ``` 當然我們在`bind`寫入參數時使用部分帶入,這裡只寫入第一個參數`小明`,第二個參數先不要帶入。 第二個參數,會依序取用第二次執行帶入的參數 :::info :bulb:這也是`bind`容易讓人混淆的地方,`fn2(1,2)`看起來像simply call,但實際`fn.bind(family,'小明')`的部分已經決定this的調用方式 ::: <br><br><br><br> ### <font class="h3">透過call實現new創建物件</font> ```javascript function MyObject(name,age){ this.name = name this.age = age } var obj = new MyObject("Mark",30) ``` <br><br> 使用 new 建立物件,其實也就是等同於: ```javascript var a = {} MyObject.call(a,"Mark",30) ``` <br><br><br><br> ### <font class="h3">嚴謹模式 strict mode</font> 使用call方式將純值給傳入 ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } fn.call(1,'小明','杰倫') ``` ![](https://i.imgur.com/AZWG9U7.jpg) 數字`1`傳入之後會以「建構式物件」方式呈現 <br> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } fn.call('文字','小明','杰倫') ``` ![](https://i.imgur.com/3xugGdp.jpg) 如果以文字傳入,一樣會是「建構式物件」方式呈現 <br> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function fn(para1, para2) { console.log(this, para1, para2); } fn.call(undefined,'小明','杰倫')//window,小明,杰倫 ``` 如果傳入`undefined`,this則會指向`window` :::info :bulb:使用call呼叫函式的this時,若是在「非嚴格模式」下傳入的是`null`、`undefined`,將會被替換為全域變數,而原使型態的值將會被以建構式物件方式封裝,如前面使用數字`1`跟`文字`,就會以建構式方式被封裝。 ::: <br> 因為javascript是屬於相對寬鬆的程式,因此許多不嚴謹的程式碼都可以被執行,在ES5之後就提供開發者這種語法受限的模式(嚴格模式) - 加入`'use strict'`即可運作 - 並不會影響不支援嚴格模式的瀏覽器 - 可依據執行環境設定`'use strict'` - 透過拋出錯誤的方式消除一些安靜的錯誤(防止小錯誤) - 禁止使用一些有可能被未來版本ECMAScripr定義的語法,也就是說你在ES5的環境下加入`'use strict'`,ES6,ES7可能被定義的語法,會禁止你來使用。 :::info 嚴格模式在每個瀏覽器的執行結果都不太一樣 ::: ```javascript (function () { 'use strict'; }) ``` 這邊使用即時函式來限制執行環境,當我在裡面加入`'use strict'`的字串時候,這一段執行環境就會進入嚴格模式。 <br> ```javascript (function () { 'use strict'; a ='小明' })//會報錯 ``` 在嚴格模式下,會要求你必須先宣告變數,才能夠賦予值 ### <font class="h4">範例:</font> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function callStrict(para1, para2) { 'use strict'; console.log(this, typeof this, para1, para2); } callStrict.call(1, '小明', '杰倫')//1 'number' '小明' '杰倫' ``` 這裡使用嚴格模式的話,數字`1`就不會被以建構式物件方式封裝,而是型別會維持原來的型別(數字型別的`1`) <br> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function callStrict(para1, para2) { 'use strict'; console.log(this, typeof this, para1, para2); } callStrict.call(undefined, '小明', '杰倫')//undefined, undefined, '小明' '杰倫' ``` 這邊試這帶入`undefined`使用嚴格模式,this就不會指向window,而維持`undefined` <br> ```javascript var myName = '真心話大冒險' var family = { myName: '小明家', } function callStrict(para1, para2) { 'use strict'; console.log(this, typeof this, para1, para2); } callStrict('小明', '杰倫')//undefined, undefined, '小明' '杰倫' ``` 這邊如果直接使用`callStrict()`來執行(simple call的方式) 一般模式下使用simple call方式呼叫this,this會指向`window` 而==在嚴格模式下,原本的window就會變成`undefined`== :bulb:其實在使用簡易呼叫時,就如同使用call的方法,只不過簡易呼叫並不會傳入this這個參數,因此this的參數是使用undefined來替代,在進入嚴格模式下`undefined`就會維持原來的`undefined` :::info 所以簡易呼叫盡可能不要去調用this,因為它的本質就是`undefined` ::: <br><br><br>

    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