Javascript 在for循环中声明两个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14696261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring two variables in a for loop
提问by Celeritas
Is it possible to declare two variables in the initialization part of a for loop? I want to call a function on each character of a string.
是否可以在 for 循环的初始化部分声明两个变量?我想对字符串的每个字符调用一个函数。
for(var i = 0, c = aString.charAt(i); i < aString.length; i++){//problem here: not itterating
alert("c: "+c)
func1[typeOfChar(c)]++
}
The problem is the string isn't being itterated in the sense cis always the first letter of the string.
The alertwas just for trouble shooting purposes, by the way.
问题是字符串没有被迭代,因为c它总是字符串的第一个字母。alert顺便说一下,这只是为了排除故障。
I'm curious, how come cdoesn't need the varkeyword when being declared?
我很好奇,为什么声明时c不需要var关键字?
UPDATE: got it working. I wasn't going to ask but I notice edits are still being made, I'm used to not using the semi-colons as they are optional. How can a for loop be written without them? I don't add them because I see it as the less the simpler, or do they improve readability?
更新:让它工作。我不打算问,但我注意到仍在进行编辑,我习惯不使用分号,因为它们是可选的。如果没有它们,如何编写 for 循环?我不添加它们是因为我认为它越少越简单,或者它们是否提高了可读性?
回答by Benjamin Gruenbaum
You'd like cto change at every iteration, not to declare it at the start of the loop, try
您想c在每次迭代时更改,而不是在循环开始时声明它,请尝试
var i,c;
for(i = 0,c=aString.charAt(0); i < aString.length; ++i, c = aString.charAt(i)){
alert("c: "+c)
func1[typeOfChar(c)]++
}
For what it's worth I don't think it makes very readable code, I would put it in the first line.
对于它的价值,我认为它不会编写非常可读的代码,我会将它放在第一行。
Here is some information on the comma operatoryou're using.
以下是有关您正在使用的逗号运算符的一些信息。
Also note that javascript has no block scoping for for loops, so you're actually declaring iand cat the top of the current scope (this is usually the top of the current function, or the top of the global scope).
另请注意,javascript 没有 for 循环的块作用域,因此您实际上是在当前作用域的顶部声明i和c(这通常是当前函数的顶部,或全局作用域的顶部)。
Here is a fiddle: http://jsfiddle.net/maWua/
这是一个小提琴:http: //jsfiddle.net/maWua/
回答by Fletcher Bach
Simple way to include multiple incrementing variables in a for loop without nesting. This example declares 3 variables.
在 for 循环中包含多个递增变量而不嵌套的简单方法。此示例声明了 3 个变量。
for (var i = 0, j = 1, n = 2; i < 50, n < 50; i = i + 3, j = j + 3, n = n + 3){
console.log("variable i: " + i);
console.log("variable j: " + j);
console.log("variable n: " + n);
}
see codepen here
在此处查看代码笔
回答by Davin Tryon
You should just place c = aString.charAt(i);inside the body of the loop.
您应该只放置c = aString.charAt(i);在循环体内部。
For example:
例如:
for(var i = 0; i < aString.length; i++){
c = aString.charAt(i);
alert("c: "+c);
func1[typeOfChar(c)]++
}
回答by Davin Tryon
In this case, because cis only dependent upon i(and an invariant) and it is notused the conditional of the loop, I recommend removing it from the loop construct:
在这种情况下,因为c仅依赖于i(和一个不变量)并且它不用于循环的条件,我建议将它从循环构造中删除:
// (Keep variable hoisting in mind)
for(var i = 0; i < aString.length; i++){
var c = aString.charAt(i);
alert("c: "+c);
// ..
};
(The problem with the original is that it never updatedthe value of c.)
(原始的问题在于它从未更新的值c。)
回答by Bergi
Yes, it is possible by using a multiple varstatement, and you did successfully. However, assigning to it only once in the initialisation statement will not make it change.
是的,可以通过使用multiple varstatement,并且您成功了。但是,在初始化语句中只给它赋值一次不会改变它。
You will either need to do it once before the loop and after each loop turn
您需要在循环之前和每个循环之后执行一次
for (var i=0, c=str.charAt(i); i<str.length; c=str.charAt(++i)) …
or you do it before each turn in the condition
或者你在每个回合之前做这个条件
for (var i=0, c; c=str.charAt(i), i<str.length; i++) … // comma operator
for (var i=0, c; c=str.charAt(i); i++) … // empty string as condition
or just move it inside the loop
或者只是将它移动到循环内
for (var i=0, c; i<str.length; i++) { c=str.charAt(i); … }
回答by Paulo
Here:
这里:
c = aString.charAt(i)
iis always zero, so it's not going to work as expected. Initialization occurs once and you are trying to get the char during init when i has just been initialized to 0.
i始终为零,因此它不会按预期工作。初始化发生一次,当我刚刚被初始化为0.
回答by hien
Another option is using while,in some case like that it's clearer:
另一种选择是使用while,在某些情况下,它更清晰:
var i = 0;
while(i < aString.length){
c = aString.charAt(i);
alert("c: "+c)
func1[typeOfChar(c)]++
i++;
}

