我想访问一个变量,其目的是聚合迄今为止在提供给 forEach 的回调中看到的记录。像这样的事情:
var myfn = function() {
var aggregate_val = [];
someObj.someFunction(
arg1,
arg2,
(function() {
....
some_array.forEach(function(e) {
this.aggregate_val.push(e.some_property);
}, this);
}).bind(this)
);
}
为什么这不起作用?
最佳答案
您不需要使用 this
来引用数组 aggregate_val
。试试这个代码:
var myfn = function() {
var aggregate_val = [];
some_array.forEach(function(e) {
aggregate_val.push(e.some_property);
});
console.log(some_array) // I added this so you can see the value of some_array
}
关于javascript - 访问 forEach 回调内部的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31440596/