-
order for..in loops differ
Can someone explain this for me? I'm using a for..in loop. Why does this give the order I expect it to:
Code:
var persoon:Object = {naam:"Rembrandt", geslacht:"man", geboortedatum:"15 juli 1606", geboorteplaats:"Leiden" };
for (var i in persoon) {
trace("persoon."+i+" = "+persoon[i]);
}
Outputs:
persoon.naam = Rembrandt
persoon.geslacht = man
persoon.geboortedatum = 15 juli 1606
persoon.geboorteplaats = Leiden
But when I do it like this it outputs it backwards?
Code:
var persoon = new Object();
persoon.naam = "Rembrandt";
persoon.geslacht = "man";
persoon.geboortedatum = "15 juli 1606";
persoon.geboorteplaats = "Leiden";
for (var i in persoon) {
trace("persoon."+i+" = "+persoon[i]);
}
Outputs:
persoon.geboorteplaats = Leiden
persoon.geboortedatum = 15 juli 1606
persoon.geslacht = man
persoon.naam = Rembrandt
-
Senior Member
It is because in Flash, a for..in loop iterates properties by reversing the index.
-
But why does it do it forwards in the first example and backwards in the 2nd?
-
Senior Member
oh, sorry, I didn't pay enough attention to the first example.
It seems that if you are to declare every propety to your object at once, the index will be displayer in the good order, but, if you ad some other property afterwards, this last one will be indexed first. Try mixing the two methods to see how it works.
But why is it happening like this, I don't have a clue.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|