AS3 - for (... in ...) 与 for every (... in ...)
以下代码执行完全相同的操作。 for every
和 for (... in ...)
之间有区别吗?
var bar:Array = new Array(1,2,3);
for (var foo in bar){
trace(foo);
}
for each (var foo2 in bar){
trace(foo2);
}
The following code does the exact same thing. Is there a difference between for each
and for (... in ...)
?
var bar:Array = new Array(1,2,3);
for (var foo in bar){
trace(foo);
}
for each (var foo2 in bar){
trace(foo2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,他们不做完全相同的事情。
for..in 循环的输出是
而 for every..in 循环的输出是
for..in 循环迭代数组或属性名称的键/索引 一个对象。 foreach..in 循环迭代值。您得到上述结果是因为您的
bar
数组的结构如下:No, they do not do the exact same thing.
The output of your for..in loop is
While the output of your for each..in loop is
A for..in loop iterates through the keys/indices of an array or property names of an object. A for each..in loop iterates through the values. You get the above results because your
bar
array is structured like this:这里的一些混乱是您在数组中使用数字。让我们切换到字符串,看看会发生什么。
现在你的输出是:
如你所见,for-in循环遍历索引(或键),for-each-in循环遍历价值观。
Some of the confusion here is that you are using numbers in your array. Let's switch to strings and see what happens.
Now your output is:
As you can see, for-in loops over indexes (or keys), and for-each-in loops over values.