Day 18:this 關鍵字 (4)
函數執行環境下 (Function Context) (續)
7. 回呼函數 (Callback Function) 裡的 this
7.1. 簡單呼叫 Callback Function (一般模式)
var name = "Hi I am Global";
function sayHi(){
return this.name;
}
var hero = {
name: "Hi I am a Hero",
act: function(cbk){
return cbk();
}
};
console.log( sayHi() ); // Hi I am Global
console.log( hero.act(sayHi) ); // Hi I am Global7.2. 簡單呼叫 Callback Function (嚴謹模式)
7.3. 用 apply() / call() 將物件本身傳入 Callback Function
總結 this
要判斷 this 是誰,就看是誰呼叫
函數執行環境下,判斷 this 的公式秘笈
小補充:this 不是變數
this 不是變數References
Last updated
Was this helpful?