-
普通调用
this – > window
function fn(){ console.log(this); // window } fn();
-
对象调用
this – > obj
function fn(){ console.log(this) // obj } var obj = { f : fn }; obj.f();
-
构造函数调用
this – > 构造函数的实例
function Person(name){ this.name = name; //this --> 实例 } var p1 = new Person('zs');
-
定时器中的回调函数
this – > window
setTimeout(function(){ console.log(this)// window },1000);
-
事件处理函数调用
this --> 事件源
box.onclick = function(){ console.log(this); // box }
总结: this的指向是在函数调用时决定的. 谁调用这个函数,this指向谁