4

I have a JavaScript function that needs to return different objects depending on some variables.

return {
  source: fSource,
  a: readArray(),
  b: readArray()
};

Is there any guarantee that the interpreter assigns the first return value of readArray to a and the second to b? I could easily rewrite it to:

var tmp = {
  source: fSource,
};
tmp.a = readArray();
tmp.b = readArray();
return tmp;

But I have a lot of such code and the first notation is quite pretty.

I know that the order of object properties might change but I doubt this affects my case.

Update:

To clarify my concerns:

var a = {
  foo: fn1(),
  bar: fn2()
};

I don't care about the order of foo and bar in the object. But is it possible that fn2 is called before fn1?

just lerning
  • 876
  • 5
  • 20
  • This affects any cases in JavaScript objects. Initially object properties are not ordered and browsers may treat it differently. If you care of the order you'd better use array. – VisioN Oct 11 '13 at 14:32
  • @VisioN I don't care about the order of properties, I care about the order of invocations. – just lerning Oct 11 '13 at 14:34
  • Ah, that makes more sense. The order of invocations should be the same. – VisioN Oct 11 '13 at 14:35
  • Potential duplicates: http://stackoverflow.com/questions/17437965/javascript-object-initialization-and-evaluation-order and http://stackoverflow.com/questions/16200387/are-javascript-object-properties-assigned-in-order – hopper Oct 11 '13 at 14:50

1 Answers1

1

Answering on your update...

No, it is not possible.

The function invocations will go in the same order as you have declared in the code.

VisioN
  • 143,310
  • 32
  • 282
  • 281