this的几个例子,没理解,求指点

第一个比较好理解,箭头函数的this用法,在定义时绑定的,所以应该是输出21,foo.call(...)再去绑定无效

this.id = 21; 
var foo= ()=>{console.log(this.id)};
this.foo.call({ id: 42 });

但是第二个为什么绑定有效,这个没理解

this.id3 = 21; 
function foo3() { 
    var f=() => { console.log('id3:', this.id3); };
    f(); 
} 
foo3.call({ id3: 42 });
阅读 1.9k
2 个回答

绑定的不是 f 的 this ,而是 foo3 的。

f 用的是 foo3 的 this 。

call apply bind 就是改变this指向的。
fool3.call('这是值就是使用时的this')

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题