AS3 for each … in vs for … in

December 16th, 2011 by Kawika Heftel

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!

Related posts:

  1. Ira Glass on Creativity
  2. 3D MovieClips with Flex and ActionScript 3

Posted in Technology, Thoughts

Tags: , , , , ,

Leave a Reply