2

For example:

var f=function(str){
  console.log(str);
  return str;
};
var obj={
  a:f('value A'),
  b:f('value B'),
};

//some other code

does it guarantee

value A
value B

instead of

value B
value A

is printed?

Note: I'm asking the order of execution of f(), not the order of keys of Object.keys(obj).

ocomfd
  • 4,010
  • 2
  • 10
  • 19
  • 1
    No it does not, objects never guarantee order – Sterling Archer Feb 02 '18 at 02:04
  • Yes, but only in native es6 compliant implementations. So don't count on it. – Jared Smith Feb 02 '18 at 02:04
  • 5
    Yes it does, and not just in ES6. You're asking about the order of execution of the functions, and its order is guaranteed. Comments above are confusing this question with the order of visitation of properties during enumeration. –  Feb 02 '18 at 02:05
  • @Barmar: That's not a duplicate. –  Feb 02 '18 at 02:08
  • @SkinnyPete Yes, misunderstood the question, reopened. – Barmar Feb 02 '18 at 02:09
  • 1
    @charlietfl: The `f` function could return an object that contains an index that is incremented on each call, thereby recording the creation order of the properties and establishing an actual, reliable order for sorting or other purposes. –  Feb 02 '18 at 02:19
  • The [relevant bit of the specification](https://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation) essentially says to (recursively) handle the properties before the final comma, then the property after that comma, which implies a standardized order. – Scott Sauyet Feb 02 '18 at 02:31
  • I'll +1 this question. It considers different aspects of event execution & ordering and object/property creation. – vol7ron Feb 02 '18 at 02:52

1 Answers1

1

Even though this is clearly a duplicate of Does JavaScript Guarantee Object Property Order?, I'll go ahead and answer anyway.

There is zero guarantee of order in your "unordered collection of properties". However with modern implementations of Javascript, you can expect that the properties will usually be in order as you defined them.

As long as the expected order is preferable, but not critical, it is reasonable in most cases to just assume they will be in order. But if your business logic depends on that order to be guaranteed, then you definitely need to rethink your strategy.

Okay. I see now what you are actually asking. In this particular context, the answer is "yes and no".

Your functions are being called statically as your object is being defined. So the function defining property a will always be called before the function defining property b.

However, console.log is an asynchronous function, and you cannot rely on it to log value A before it logs value B

skylize
  • 1,401
  • 1
  • 9
  • 21
  • 2
    Read the comments above. The concept of an unordered collection has no bearing on the order of the evaluation of its members in its initializer. –  Feb 02 '18 at 02:21
  • ...take this example: `var i = 0; var obj = {a:++i, b:++i, c:++i};` The key/value pairs will reliably be `a:1`, `b:2`, and `c:3`. Enumerating the object may not visit them in that order, but the pairings will be sure. Imagine how confusing it would be if the initializing order wasn't guaranteed. –  Feb 02 '18 at 02:25