1

Possible Duplicate:
Does JavaScript Guarantee Object Property Order?

I notice that in Javascript (on Chrome, at least) objects remember the order of their members. For example, in

var foo = { x: 1, y: 2 }
var bar = { y: 2, x: 1 }

foo and bar are distinguishable by the order the keys appear when I iterate over them.

My questions are:

  1. Is this behavior specified by the standard, or should it be considered an implementation detail?
  2. If it is specified by the standard, is there a way to change the order?
Community
  • 1
  • 1
luqui
  • 57,324
  • 7
  • 134
  • 191
  • What do you need to change the order for? – zerkms May 04 '12 at 08:04
  • @zerkms, seriously? My application uses a sorted dictionary where the sort order is modifiable. I can simulate it by storing the order alongside the dictionary, I was just wondering whether I needed to. – luqui May 04 '12 at 08:11

1 Answers1

3

Objects properties are not sorted. The behaviour is implementation-dependent.

However, most JS engines seem to keep the order that was used in an object literal nowadays.

V8/Chrome didn't do so some time ago though.

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610