AS3: List Dynamic Object Properties and Values
A little known feature of AS3 is the ability to not only list an object’s properties but their values as well. They expose this functionality in a really weird way though. Both the for and for each key words yield different results. As a rule of thumb I would suggest always using for because it gives you not only the properties of the dynamic object, but also the values:
var obj:Object = new Object();
obj.foo = "bar";
obj.hello = "goodbye";
for(var key:* in obj)
{
trace(key + ": " + obj[key]);
}
// Output:
// foo: bar
// hello: goodbye
for each(var value:* in obj)
{
trace(value + ": " + obj[value]);
}
// Output:
// bar: undefined
// goodbye: undefined
You’ll notice that the first keyword usage, just the for, populates the property name into the iterator “key” , so you can access the property using the Array notation. The second keyword, for each populates the iterator “value” with the value of the properties. Hardly useful when you are serializing a dynamic object and would like to know what property the value belongs to!
So I would suggest always using for unless you really want to use for each for a specific reason.