Day 22:箭頭函數 (Arrow Functions) 的 this 和你想的不一樣 (2)
3. 顯性函數綁定 (Explicit Function Binding)
3.1. Function.prototype.bind() 篇
傳統函數
var getFullName = function() {
return this.firstName + " " + this.lastName;
}
var firstName = "One", lastName = "Jar";
var introIronMan = getFullName.bind( { firstName: "Tony", lastName : "Stark" } );
var introCaptainAmerica = getFullName.bind( { firstName: "Steven", lastName : "Rogers" } );
console.log(getFullName()); // "One Jar"
console.log(introIronMan()); // "Tony Stark"
console.log(introCaptainAmerica()); // "Steven Rogers"Arrow Functions
3.2. Function.prototype.apply() / Function.prototype.call() 篇
傳統函數
Arrow Functions
4. 函數作為建構子
傳統函數
Arrow Functions
5. 回呼函數 (Callback Function) 裡的 this
5.1. 簡單呼叫 Callback Function
傳統函數
Arrow Functions I
Arrow Functions II
5.2. 用 apply() / call() 將物件本身傳入 Callback Function
傳統函數
Arrow Functions I
Arrow Functions II
總結
一句話總結傳統函數和箭頭函數在 this 判斷上的差別
this 判斷上的差別既然一句話就能講完,吃飽太閒看這麼多範例?
References
PreviousDay 21:箭頭函數 (Arrow Functions) 的 this 和你想的不一樣 (1)NextDay 23:ES6 物件實字威力加強版 (Enhanced Object Literals)
Last updated
Was this helpful?
