The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are not kept in any particular order, so properties will appear in a random order):
var myObj:Object = {x:20, y:30};
for (var i:String in myObj) {
trace (i + ": " + myObj[i]);
}
// Output:
// x: 20
// y: 30
You can also iterate through the elements of an array:
var myArray:Array = ["one", "two", "three"];
for (var i:String in myArray) {
trace (myArray[i]);
}
// Output:
// one
// two
// three
What you cannot do is iterate through the properties of an object if it is an instance of a custom class, unless the class is a dynamic class. Even with instances of dynamic classes, you will only be able to iterate through properties that are added dynamically.
0 komentar:
Post a Comment