A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: order for..in loops differ

  1. #1
    Senior Member
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    326

    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

    Illustration | Animation | Web Banners | Graphic Design
    Ducklord Studio

  2. #2
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    It is because in Flash, a for..in loop iterates properties by reversing the index.

  3. #3
    Senior Member
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    326
    But why does it do it forwards in the first example and backwards in the 2nd?

    Illustration | Animation | Web Banners | Graphic Design
    Ducklord Studio

  4. #4
    Senior Member Ovaire d'Oz's Avatar
    Join Date
    May 2006
    Location
    France
    Posts
    148
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center