Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Javascript Object Properties assigned in order?

Tags:

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 = {
            a:f(),
            b:f(),
            c:f()
          };

Is it guaranteed that foo.a will be 1, foo.b will be 2, and foo.c will be 3? I know that JS doesn't guarantee order when you iterate over an object, what about assignment?

Is it specified in the JS specification somewhere? I'm only asking for educational reasons.

Thanks.

like image 820
jdw Avatar asked Apr 24 '13 19:04

jdw


People also ask

What is an object property in JavaScript?

JavaScript | Object Properties. Object properties are defined as a simple association between name and value. All properties have a name and value is one of the attributes linked with the property, which defines the access granted to the property. Properties refer to the collection of values which are associated with the JavaScript object.

Is the Order of properties in JavaScript objects predictable?

When you started writing JavaScript a few years ago, you might have heard the statement that the order of properties in JS objects is not predictable. I never came across an odd and unusual order of properties, but I always followed the rule "never rely on property order".

Are JavaScript object keys ordered?

To illustrate that JavaScript object keys are not ordered, let’s compare them to an array: a simple list of items in a specific order. JavaScript arrays have a defined order from index 0 to the last item in the list, and items added to the array using .push () stay in the order they are added in.

What is the Order of property order in normal objects?

Property order in normal Objects is a complex subject in JavaScript. While in ES5 explicitly no order has been specified, ES2015 defined an order in certain cases, and successive changes to the specification since have increasingly defined the order (even, as of ES2020, the for-in loop's order). Given is the following object:


2 Answers

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.

like image 96
Karoly Horvath Avatar answered Sep 18 '22 05:09

Karoly Horvath


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.

Edit: Found it

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.

like image 26
Sébastien Renauld Avatar answered Sep 19 '22 05:09

Sébastien Renauld