Say I have an object which assigns properties based off the return value of a function:
var i = 0;
var f = function() { return ++i; }
var foo = {
Standard ECMA-262 (5.1) - Section 11.1.5 - Object Initialiser
The production PropertyNameAndValueList : PropertyNameAndValueList , PropertyAssignment is evaluated as follows:
1. Let obj be the result of evaluating PropertyNameAndValueList. 2. Let propId be the result of evaluating PropertyAssignment. ... 5. Call the [[DefineOwnProperty]] internal method of obj with arguments propId.name, propId.descriptor, and false. 6. Return obj.
So yes, the order is enforced by the standard.
Yes, you are guaranteed that a will be 1, b will be 2, etc. Each f()
will be interpreted in the order you wrote them.
Assignment is always in order. It is just the way the code is found, interpreted and executed.
From the ECMAScript 6 wiki, which will define the new version of JS:
When a scope (Block, FunctionBody, Program, ModuleBody, etc.) is entered, the variables declared by all immediately contained function and class declarations are bound to their respective functions and classes. Then all class bodies are executed in textual order. A class body defines and initializes class-wide properties once when the class definition is evaluated. This includes properties on the constructor function (the “class” itself) and on its prototype property. These initializations happen in textual order.
Your source has arrived! JavaScript object properties are initialized in textual order on objects. Arrays do not (currently) always follow this rule.
Source: http://wiki.ecmascript.org/doku.php?id=harmony:classes
I will edit this post when I find the reference in ECMAScript 5, though I am certain it is there.
ECMAScript 5 does have it: http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.7 .
If an implementation defines a specific order of enumeration for the for-in statement, that same enumeration order must be used to order the list elements in step 3 of this algorithm.
This defines the calls to DefineOwnProperty
and therefore the position of the properties in the internal table.
I don't have an official source to quote, but let's just use a little common sense.
What if assignment wasn't done in order? You could never know which value would be assigned to which property, making the object structure useless (at least when using the object literal syntax).
It makes no difference if it's a function call, or a primitive literal, or some other value. If it wasn't guaranteed to happen in order, it just wouldn't work.
After doing a quick search of the term left to right
in ECMAScript 5, here are some results if it helps you:
7 Lexical Conventions
The source text of an ECMAScript program is first converted into a sequence of input elements, which are tokens, line terminators, comments, or white space. The source text is scanned from left to right, repeatedly taking the longest possible sequence of characters as the next input element.
11.8.5 The Abstract Relational Comparison Algorithm
...It is necessary because ECMAScript specifies left to right evaluation of expressions.
Annex D (informative) Corrections and Clarifications in the 5th Edition with Possible 3rd Edition Compatibility Impact
ECMAScript generally uses a left to right evaluation order, however the Edition 3 specification language for the > and <= operators resulted in a partial right to left order. The specification has been corrected for these operators such that it now specifies a full left to right evaluation order.