Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 - for (... in ...) vs for each (... in ...)

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); } 
like image 477
apscience Avatar asked Aug 21 '11 10:08

apscience


People also ask

What are loops in action script?

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.

What is as3 programming?

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.

How many loops are present in ActionScript?

There are three kinds of loops in ActionScript, the for loop, the while loop, and the do loop.


1 Answers

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; 
like image 101
BoltClock Avatar answered Sep 21 '22 01:09

BoltClock