我有一个这样的数组:
let arr = [
{
num: 10,
reload: () => {
this.num = 30;
}
},
{
num: 40,
reload: () => {
this.num = 20;
}
}
]
然后我正在尝试运行此代码
arr.forEach(item => {
item.reload()
})
我希望数组的第一个对象有num= 30 和第二个作为num = 20
this 没有看到工作..
let arr = [
{
num: 10,
reload: function(){
this.num = 30;
}
},
{
num: 40,
reload: function(){
this.num = 20;
}
}
]
arr.forEach(item => {
item.reload()
})
console.warn(arr);
let arr = [
{
num: 10,
reload(){
this.num = 30;
}
},
{
num: 40,
reload(){
this.num = 20;
}
}
]
arr.forEach(item => {
item.reload.call(item)
})
console.log(arr)
箭头函数this指向问题 用function(){} 能将this指向自己内部