or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
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.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
this
tags:
javascript
、advanced
、concept
this
在 javascript 通常用在物件導向或是事件監聽當中比較直觀而且確實應該用在那邊,如果用在其它地方可能就沒有什麼必要,而且還會造成易讀性差。call 和 apply 的差別
call
和apply
如果要傳this
的設定值的話,用法都是一樣的,在第一個參數輸入設定值即可:但是如果要傳入引數到 function,我們看下面這個例子:
call
的第二個引數(1)對應到 function 的第一個參數(a)、第三個引數(2)對應到 function 的第二個參數(b),以此類推;而apply
的第二個引數也是對應到 function 的參數,但是它必須傳入陣列才行,第一個陣列值(1)與 function 第一個參數(a)對應,以此類推。在物件導向其他地方使用 this
this
的值會被定義為如何呼叫它,使用什麼方式呼叫它,而不是取決於this
在程式碼的位置與何時被定義。可以使用call(被呼叫的 function 前面的 object)
來看比較容易,比如:上面的例子意思是可以把
obj.inner.test()
的obj.inner
(也就是呼叫test()
的 object)放到call
的參數當中來看,會和原本的結果一模一樣。因此使用call()
一眼就可以看出this
的值為何(這邊是obj.inner
)。bind
bind
方法可以綁定 object 的this
結果,也就是說不管怎麼 call object 結果都一樣,比如:bind
的結果會回傳一個新的 funciton 給bindTest
,所以才會有怎麼 call 都會一樣的情形。參考文章:
淺談 JavaScript 頭號難題 this:絕對不完整,但保證好懂