1) 代表全局对象global var x=0; function fn(){ this.x=1 } console.log( fn() ) ; // 1 此时的this作为全局对象,调用全局变量 x var obj={}; obj.fn.apply( obj ); // 1 // this指向obj
2) 在构造函数中(constructor),this指当前对象 function Parent(){ this.y=1; } var obj=new Parent(); console.log( obj.y ) ; // 1 此时的this指obj对象
3) 定时器中的 this 指的是 window对象 setTimeout(function(){ // 在定时器中使用this时,this 指window对象 },1000)
4) apply中使用this,this指向的是apply中的第一个参数。 var y=0; function test(){ console.log(this.y); } obj.y=1; obj.fn=test; obj.fn.apply(); // 0 this指向 window