Are Javascript Object Properties assigned in order? -
say have object assigns properties based off return value of function:
var = 0; var f = function() { return ++i; } var foo = { a:f(), b:f(), c:f() };
is guaranteed foo.a 1, foo.b 2, , foo.c 3? know js doesn't guarantee order when iterate on object, assignment?
is specified in js specification somewhere? i'm asking educational reasons.
thanks.
standard ecma-262 (5.1) - section 11.1.5 - object initialiser
the production propertynameandvaluelist : propertynameandvaluelist , propertyassignment evaluated follows:
1. let obj result of evaluating propertynameandvaluelist. 2. let propid result of evaluating propertyassignment. ... 5. call [[defineownproperty]] internal method of obj arguments propid.name, propid.descriptor, , false. 6. return obj.
so yes, order enforced standard.
Comments
Post a Comment