3
我有2箱子可见性的“本”中箭功能
const test = {
foo: function(){
this.bar();
},
bar: function(){
console.log('bar');
}
}
test.foo();
在这种情况下
,一切正常。
const test = {
foo:() => {
this.bar();
},
bar:() => {
console.log('bar');
}
}
test.foo();
在第二种情况下,我得到错误:
Uncaught TypeError: Cannot read property 'bar' of undefined
我知道我可以在foo
功能写道test.bar()
,但我想知道为什么this
在这种情况下箭头功能范围无法使用。
胖箭头函数中的作用域不会调用它所调用的对象的上下文 –