AS3 - 为(...的...)对每个(... ...中) [英] AS3 - for (... in ...) vs for each (... in ...)

查看:100
本文介绍了AS3 - 为(...的...)对每个(... ...中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code做同样的事情。是否有每个和的之间的的差异(在... ...)

  VAR吧:阵列=新阵列(1,2,3);

对(巴VAR富){
    跟踪(富);
}

每个(巴VAR foo2的){
    跟踪(foo2的);
}
 

解决方案

没有,他们的没有的做同样的事情。

您for..in循环的输出是

  0
1
2
 

当你的for each..in循环输出

  1
2
3
 

通过键/对象的数组或属性名的指数 for..in循环迭代。一种通过在值each..in循环迭代。你得到上面的结果,因为你的阵的结构是这样的:

 栏[0] = 1;
扎[1] = 2;
扎[2] = 3;
 

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);
}

解决方案

No, they do not do the exact same thing.

The output of your for..in loop is

0
1
2

While the output of your for each..in loop is

1
2
3

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:

bar[0] = 1;
bar[1] = 2;
bar[2] = 3;

这篇关于AS3 - 为(...的...)对每个(... ...中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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