javascript while 循环在节点和浏览器中给出不同的结果 [英] javascript while loop giving different results in node and browser
问题描述
我正在执行以下看似简单的代码
I am executing the following seemingly straightforward code
var number = 0;
while (number <= 12) {
console.log(number);
number = number + 2;
}
我在浏览器和 Node.js 中得到了不同的结果.当我在 Firefox(v. 32.0.3) 上的 Firebug(v 2.0.4) 控制台中运行它时,我得到的结果是
and I am getting different results in the browser and in Node. When I run it in the Firebug(v 2.0.4 ) console on Firefox(v. 32.0.3) the result I get is
02468101214
0 2 4 6 8 10 12 14
这不是我预期的结果.
在 Node 中,相同的代码给了我正确的答案
In Node, the same code gives me the correct answer which is
024681012
关于浏览器中的行为,我有什么遗漏吗???
Is there anything I'm missing regarding the behaviour in the browser???
提前致谢.
推荐答案
如果您在 Firebug 的控制台中运行脚本,那么它会评估代码.因此,它会计算 while 循环中最后一个数字的值(现在是 14)并将其打印出来.它实际上将 number
的值打印了 8 次,但将它们分组为 1 次打印输出.
If you run a script in Firebug's console, then it will evaluate the code. So it's evaluating the value of the last number in the while loop (which is now 14) and prints that out. It's actually printing out the value of number
8 times but groups them in 1 print out.
您只需在命令行中输入window"即可看到相同的效果.它正在评估它的价值并在控制台中打印出来.
You can see the same effect by just typing in "window" in the command line. It's evaluating it's value and prints it out in the console.
更多信息可以在Firebug的命令行的描述中找到.
More info can be found in the description to Firebug's command line.
这篇关于javascript while 循环在节点和浏览器中给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!