admin管理员组文章数量:1339698
The following code produces the following output. But is it possible to keep the value "8888" for "index"? In other words, how do I make the for-each variable a local one? CODE:
var index = 8888;
console.log(index);
for ( index in a) { // by the way a.length = 5
console.log(index);
}
console.log(index);
OUTPUT:
8888
0
1
2
3
4
4
The following code produces the following output. But is it possible to keep the value "8888" for "index"? In other words, how do I make the for-each variable a local one? CODE:
var index = 8888;
console.log(index);
for ( index in a) { // by the way a.length = 5
console.log(index);
}
console.log(index);
OUTPUT:
8888
0
1
2
3
4
4
Share
Improve this question
asked Apr 16, 2015 at 23:17
DomienDomien
4056 silver badges19 bronze badges
1
- 4 Use a different variable! OR wrap it in a IIFE – epascarello Commented Apr 16, 2015 at 23:17
5 Answers
Reset to default 8Just change the name of the variable.
Instead of
var index = 8888;
console.log(index);
for (index in a) { // by the way a.length = 5
console.log(index);
}
console.log(index);
Use
var index = 8888;
console.log(index);
for (i in a) { // by the way a.length = 5
console.log(i);
}
console.log(index);
When you write for (index in a)
you create a local variable index, that overides temporarily the other variable index
. You can change the name of the local variable for anything different than index
, then you won't have this problem!.
Scope it with const
or let
:
var index = 8888; // `index` is 8888 here.
for(const index in a){ // `index` is "0", then "1", "2", "3", and "4" here.
a[index];
}
// `index` is 8888 here again.
const
and let
make variables scoped to the block they are declared in: in this case a new index
variable is scoped to each iteration of the for
loop.
All of them overshadow the outside index
.
After the loop ends the index
variable declared with var
bees accessible again.
if is mandatory to use same name, you should use self-executing anonymous JavaScript functions
var index = 8888;
var a =[1,2,3,4,5];
console.log(index);
(function(){
for (var index in a) { // by the way a.length = 5
console.log(index);
}
}());
console.log(index);
See this documentation on foreach: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
You can have something like this and avoid using your own index variable (the arrayIndex variable below is local to the callback function in the forEach):
var array = ['a', 'b', 'c'];
array.forEach(function (current, arrayIndex) {
console.log(arrayIndex);
});
Scoping in Javascript is somewhat strange in that variables declared in an new scope will not shadow variables previously declared in a previous scope. Your question demonstrates the textbook example of what that means.
The most patible way to do this is to just rename your variable in your for loop:
var index = 8888;
console.log(index);
for (i in a) { // by the way a.length = 5
console.log(i);
}
The next most patible way is to use the forEach function:
var array = ['a', 'b', 'c'];
array.forEach(function (current, arrayIndex) {
console.log(arrayIndex);
});
The ECMA6 way that is officially approved but may still not be fully implemented by browsers is to use the let
keyword.
var index = 8888;
console.log(index);
for ( let index in a) { // by the way a.length = 5
console.log(a[index]);
}
console.log(index); // still 8888
From the documentation:
let vs var
When used inside a block, let limits the variable's scope to that block. Note the difference between var whose scope is inside the function where it is declared
It continues:
You can use the let keyword to bind variables locally in the scope of loops instead of using a global variable (defined using var) for that.
let
is pretty much Javascript trying to make there scoping less crazy in a backwards patible way.
本文标签: foreachjavascript variables in for eachStack Overflow
版权声明:本文标题:foreach - javascript variables in for each - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743601374a2508637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论