87,996
社区成员
发帖
与我相关
我的任务
分享 var age = 21;
var obj = {
age:1,
getAge:function(){
var fn = () => 2021 - this.age;
return fn();
}
}
console.log(obj.getAge()); //2020
var b = obj.getAge;
console.log(b()); //2000
//另一种写法就不同了:
var age = 21;
var obj = {
age:1,
getAge:function(){
return () => 2021 - this.age;
}
}
console.log(obj.getAge()()); //2020
var b = obj.getAge();
console.log(b()); //2020
哪位高手给解释下,这个箭头函数的this是怎么改变的?新手上路,多谢!!!
可以看下我以前写的这篇博客,希望对你有所帮助,https://oliver.blog.csdn.net/article/details/106241641
这是this的隐式丢失,改变this指向,函数的方法有call,bind,apply