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); }
In ActionScript, an infinite loop causes an error, as we'll see later. Our loop is infinite because it lacks an update statement that changes the value of the variable used in the test expression. An update statement typically causes the test expression to eventually yield false, which terminates the loop.
ActionScript 3 is an object-oriented programming language originally created by Macromedia Inc., which continued to evolve after being acquired by Adobe Systems. It is a superset of the ECMAScript standard (more widely known as JavaScript) with a stronger focus on classes, interfaces, and objects.
There are three kinds of loops in ActionScript, the for loop, the while loop, and the do loop.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With