AS3 for each … in vs for … in

Friday, December 16th, 2011

If you’re like me, remembering which version of the AS3 for … in loop does what is practically impossible.

So here is a reminder:

for each … in

Very simply, the for each … in loop iterates through the values of all properties associated with an object.

Example:

var obj:Object = {
  name:"Will Smith"
  occupation:"Actor"
};

for each (var prop in myObject){
trace(prop);
}

The above snippet will print the following:

Will Smith
Actor

for … in

Conversely, the for … in loop iterates through the name of each property associated with an object.

Example:

var obj:Object = {
  name:"Will Smith"
  occupation:"Actor"
};

for (var prop in myObject){
trace(prop);
}

The above snippet will print the following:

name
occupation

Hope this helps clear up some confusion!