关于this指向问题我们看代码
var name='456'
var obj={
name:'123',
say:function(){
console.log(this.name);
}
}
var obj1={
name:'789',
say:(fn)=>{
fn();
}
}
var obj2={
name:'000',
say:function(){
(function fn(){
console.log(this.name);
})();
}
}
obj1.say(obj.say); //456
这里为什么是456呢。我们可以把这段代码理解为
say:(fn)=>{
function(){
console.log(this.name)
}
}
这里我们算是直接调用所有指向windown
obj1.say=obj.say;
obj1.say(); //789
这里我们算重写了obj.say()
san:function(){
console.log(this.name)
}
由于重写了所以当前this是指向obj1自身
obj2.say(obj.say);//456
obj2.say=obj.say;
obj2.say()//000
这段代码的思想是和上面一样的
不同的是:在箭头函数里面的this是指向所调用的对象属性形式,由于调用所以 obj2.say(obj.say);是指向当前this父级的父级。