如图,框框中的this为什么会是Person对象而不是windows呢,不是说settimeout函数的执行对象是windows,this都会指代windows,那settimeout中出现的this不应该都是windows吗?
就像为什么下面这个匿名函数我绑定this,但是并没有指向person对象,还是打印的XL大写?只能通过that = this这样来转换才能改变this指向 这是为什么呢
var name="XL";
var person={
name:"xl",
showName:function(){
console.log(this.name);
},
sayName:function(){
(function(callback){
callback();
}.bind(this))(this.showName)
}
}
person.sayName(); //XL
两个地方你都是把匿名函数的
this
绑定到了person
对象上,不同的是第二个地方你是通过callback
函数打印name
,你并没有绑定这个函数的this
所以是指向window
对象的name
。