js如何判断函数或者方法中的this指向谁?四句话

1.谁作为拥有者调用它就指向谁

function a() { 
    console.log(this); 
}
var b  = {};
b.hehe = a;
b.hehe();
//这时候this指向b//常见的就是绑定事件


2.bind谁就指向谁

function a() { 
    console.log(this); 
}
var b  = {};
var c = {};
b.hehe = a.bind(c);
b.hehe();
//这时候this指向c//如果你用bind的话


3.没有拥有者,直接调用,就指向window

function a() { 
    console.log(this); 
}
a();
//this指向window




4.call谁就是谁,apply谁就是谁,其实bind就是通过call和apply实现的


其中2覆盖掉1


你可能感兴趣的:(细节研究,javascript)