<分区>
我有两个例子
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
在箭头函数中不可用在这种情况下的范围。