admin管理员组文章数量:1026989
someOperation.then(function(x) {
things.forEach(function(thing) {
//doing something with 'thing' that depends on variable 'x'
});
});
In the code above, how can I make the variable 'x' available inside the callback function? Or do I have to go back to using a for loop in this case?
someOperation.then(function(x) {
things.forEach(function(thing) {
//doing something with 'thing' that depends on variable 'x'
});
});
In the code above, how can I make the variable 'x' available inside the callback function? Or do I have to go back to using a for loop in this case?
Share Improve this question edited Apr 22, 2016 at 21:36 AyushISM asked Apr 22, 2016 at 21:29 AyushISMAyushISM 3811 gold badge7 silver badges21 bronze badges 3- 1 you have access to x as a closure – Gonzalo.- Commented Apr 22, 2016 at 21:31
- @Gonzalo.- ok I changed my code a bit. Will I still have access to x as a closure? because when I put a breakpoint inside the forEach callback function, I don't see 'x' in the list of closures. – AyushISM Commented Apr 22, 2016 at 21:39
- 2 to be listed on that list, you have to actual be using it as a closure. If not, all the variables of the universe of your js will be accesible. Just use it inside your function (i.e. print it with console.log) and you'll see it listed – Gonzalo.- Commented Apr 22, 2016 at 21:42
2 Answers
Reset to default 1It is available.
let x = {
name: 'Mike'
};
['Hello', 'Goodbye'].forEach(function(greeting) {
document.querySelector('pre').innerHTML += greeting + ', ' + x.name + '\n';
});
<pre></pre>
What you're using here is known as a closure and is a monly used feature of Javascript. Basically, any function has access to any other variable in it's parent scope.
function log(msg) {
document.querySelector('pre').innerHTML += msg + '\n';
}
var global = 'global';
function firstLevel(third) {
var first = 'first';
// `global` is in the parent scope so we can access it
log(global + ' -> ' + first);
function secondLevel() {
var second = 'second';
// Same thing with `first` here
log(global + ' -> ' + first + ' -> ' + second);
// This even works with passed in arguments
log(global + ' -> ' + first + ' -> ' + second + ' -> ' + third);
// We can even change closed over variables
first = 'fourth?';
}
secondLevel();
log(first); // Notice that `first` changed.
}
log(global);
firstLevel('third'); // Notice how `third` is used in `secondLevel`
<pre></pre>
You can pass a "thisArg" as the second parameter to forEach so for instance:
let x = { a: 123 };
things = ['foo', 'bar']
things.forEach(function(thing) {
alert( this.a + thing );
}, x);
Might be helpful depending on what you are trying to do.
someOperation.then(function(x) {
things.forEach(function(thing) {
//doing something with 'thing' that depends on variable 'x'
});
});
In the code above, how can I make the variable 'x' available inside the callback function? Or do I have to go back to using a for loop in this case?
someOperation.then(function(x) {
things.forEach(function(thing) {
//doing something with 'thing' that depends on variable 'x'
});
});
In the code above, how can I make the variable 'x' available inside the callback function? Or do I have to go back to using a for loop in this case?
Share Improve this question edited Apr 22, 2016 at 21:36 AyushISM asked Apr 22, 2016 at 21:29 AyushISMAyushISM 3811 gold badge7 silver badges21 bronze badges 3- 1 you have access to x as a closure – Gonzalo.- Commented Apr 22, 2016 at 21:31
- @Gonzalo.- ok I changed my code a bit. Will I still have access to x as a closure? because when I put a breakpoint inside the forEach callback function, I don't see 'x' in the list of closures. – AyushISM Commented Apr 22, 2016 at 21:39
- 2 to be listed on that list, you have to actual be using it as a closure. If not, all the variables of the universe of your js will be accesible. Just use it inside your function (i.e. print it with console.log) and you'll see it listed – Gonzalo.- Commented Apr 22, 2016 at 21:42
2 Answers
Reset to default 1It is available.
let x = {
name: 'Mike'
};
['Hello', 'Goodbye'].forEach(function(greeting) {
document.querySelector('pre').innerHTML += greeting + ', ' + x.name + '\n';
});
<pre></pre>
What you're using here is known as a closure and is a monly used feature of Javascript. Basically, any function has access to any other variable in it's parent scope.
function log(msg) {
document.querySelector('pre').innerHTML += msg + '\n';
}
var global = 'global';
function firstLevel(third) {
var first = 'first';
// `global` is in the parent scope so we can access it
log(global + ' -> ' + first);
function secondLevel() {
var second = 'second';
// Same thing with `first` here
log(global + ' -> ' + first + ' -> ' + second);
// This even works with passed in arguments
log(global + ' -> ' + first + ' -> ' + second + ' -> ' + third);
// We can even change closed over variables
first = 'fourth?';
}
secondLevel();
log(first); // Notice that `first` changed.
}
log(global);
firstLevel('third'); // Notice how `third` is used in `secondLevel`
<pre></pre>
You can pass a "thisArg" as the second parameter to forEach so for instance:
let x = { a: 123 };
things = ['foo', 'bar']
things.forEach(function(thing) {
alert( this.a + thing );
}, x);
Might be helpful depending on what you are trying to do.
本文标签: javascriptPassing arguments to ArrayforEach callback functionStack Overflow
版权声明:本文标题:javascript - Passing arguments to Array.forEach callback function - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745652363a2161394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论