Does JavaScript Guarantee Object Property Order? -
if create object this:
var obj = {}; obj.prop1 = "foo"; obj.prop2 = "bar";
will resulting object always this?
{ prop1 : "foo", prop2 : "bar" }
that is, properties in same order added them?
no, properties order in objects not guaranteed in javascript; need use array
.
definition of object ecmascript third edition (pdf):
4.3.3 object
an object member of type object. it unordered collection of properties each of contains primitive value, object, or function. function stored in property of object called method.
since ecmascript 2015, using the map
object alternative. map
shares similarities object
, guarantees keys order:
a map iterates elements in insertion order, whereas iteration order not specified objects.
Comments
Post a Comment