Skip to content

[javascript] bind()、call()及apply(),function borrowing與function currying

Published: at 05:42 AM

在 javascript 中,每個函式都會是一個物件 function,它們包含了幾個預設的方法 bind()、call()及apply()

在這篇文章中,我們將介紹這幾個方法的使用

若有使用過 javascript,應該知道 this 這個關鍵字的相關用法,但有時候 this 會指向可能不是你所預想的物件,如這篇文章

以下這幾個方法主要是可以用在處理 this 所指向的物件


假設我們有一個 person 的物件,其中有 getFullName的方法,並且其中的 this 將指向 person

如:

[javascript] var person={ “firstName”: “camel”, “lastName”: “chang”, “getFullName”: function() { return this.firstName+” “+this.lastName; } }; [/javascript]

接下來,我們來看看以上這三個方法實際使用的範例及用法

參考資料:

https://pjchender.blogspot.tw/2016/06/function-borrowingfunction-currying.html