just lerning
just lerning

Reputation: 866

Are object properties assigned in the same order they are declared?

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?

Upvotes: 4

Views: 76

Answers (1)

VisioN
VisioN

Reputation: 145428

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.

Upvotes: 1

Related Questions