Java for循环的确切执行顺序是什么? [英] What is the exact order of execution of Javascript's for loop?

查看:138
本文介绍了Java for循环的确切执行顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Codepen中,我对正在使用的特定for循环的确切执行顺序感到有些困惑.看看这两个循环,它们都做同样的事情:

I'm a bit stumped by the exact order of execution of a particular for loop I'm playing with, in Codepen. Check out these two loops, which both do the same thing:

var a = ['orange','apple']; 
for (var i=0; i<2;i++) {
    alert(a[i]);
}

for (var j=0, fruit; fruit = a[j++];) {
    alert(fruit);
}

您可以在此处看到以下操作: http://codepen.io/nickbarry/pen/MYNzLP /

You can see this in action here: http://codepen.io/nickbarry/pen/MYNzLP/

第一个循环是编写for循环的标准原始方法.不出所料,它会先发出橙色"警报,然后发出苹果"警报.

The first loop is the standard, vanilla way of writing a for loop. As expected, it alerts "orange", then "apple".

我使用MDN的建议编写了第二个循环(搜索成语",在这里: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript ).

I wrote the second loop using a suggestion from MDN (search for "idiom", here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript ).

它有效,但是我不太明白为什么.我对for循环的理解(在某种程度上(不止一种方式?)显然是不正确的)是: * for循环在第一次运行之前评估第二个表达式的真实性.因此,似乎当for循环评估第二个表达式的真实性时,应在设置fruit的值之前将j递增.因此,第一次通过循环,水果应该等于苹果". *循环应该只运行一次,因为在第一次遍历之后,当它再次评估第二个表达式以查看它是否仍然为真时,应该在将值返回给水果之前再次递增j,因为没有值在索引位置2,它应该返回错误的结果,并退出循环.

It works, but I don't quite understand why. My understanding of for loops, which is obviously incorrect in some way (more than one way?) is that: * The for loop evaluates the truthiness of the second expression before it runs the first time. So it seems that when the for loop is evaluating the truthiness of the second expression, it should increment j before setting the value of fruit. So the first time through the loop, fruit should equal "apple". * And the loop should only run once, because after the first time through, when it's evaluating the second expression again to see if it's still true, it should once again increment j before it returns a value to fruit, and since there is no value at index position 2, it should return a falsy result, and exit the loop.

我不了解实际情况. for循环是否在第二个表达式求值之前运行一次?但这似乎是不可能的,因为如果发生了这种情况,则警报第一次不会发出警报,因为水果还没有价值.

I don't understand what actually IS happening. Does the for loop run once before the second expression is evaluated? But that seems impossible, because if that happened, the alert would alert nothing the first time through, since fruit wouldn't yet have a value.

此外,MDN文章的作者似乎喜欢编写循环的第二种方法-是否有理由认为这种方法更好?我猜它使用的字符更少,这很好.跳过for循环中的第三个表达式是否可以节省大量时间?还是仅仅是它很聪明,因为它很聪明"之类的东西?

Also, the author of the MDN article seemed to like the second way of writing the loop - is there a reason that way is better? It uses fewer characters, which is good, I guess. Does skipping the third expression in the for loop save significant time? Or is it just a "it's cool because it's clever" sort of thing?

推荐答案

for (var j=0, fruit; fruit = a[j++];) {
    alert(fruit);
}

在伪代码中等于:

initialize j = 0 and fruit = undefined

assign fruit = a[j] (j = 0, fruit = 'orange')
j = j + 1 (j = 1)
check fruit for being truthy (true)

alert(fruit) ('orange')

assign fruit = a[j] (j = 1, fruit = 'apple')
j = j + 1 (j = 2)
check fruit for being truthy (true)

alert(fruit) ('apple')

assign fruit = a[j] (j = 2, fruit = undefined)
j = j + 1 (j = 3)
check fruit for being truthy (false)

exit loop

重要说明:

后缀一元++运算符的工作方式为:

The postfix unary ++ operator works as:

  1. 我们返回变量的当前值
  2. 我们增加变量的值

跳过for循环中的第三个表达式是否可以节省大量时间?

Does skipping the third expression in the for loop save significant time?

它根本不保存任何东西

此外,MDN文章的作者似乎喜欢编写循环的第二种方法-是否有更好的方法?

Also, the author of the MDN article seemed to like the second way of writing the loop - is there a reason that way is better?

这在任何方面都不是更好.作者只是认为这很酷,而作者却喜欢冷静(虽然他们不喜欢).

It's not better in any way. Author just thinks it's cool and author likes to be cool (while they are not).

这篇关于Java for循环的确切执行顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆