您的位置:首页 > Web前端 > JavaScript

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

2016-01-13 18:39 651 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: