6

I have a function like that:

parsers[1] = function(buf) {
 return {
 type: "init",
 name: buf.readUTF8String(),
 capacity: buf.readUInt32(),
 port: buf.readUInt16()
 };
}

Do I have any guarantee that name, capacity, and port will be initialized one after the other? Otherwise, the buffer will be read in the wrong order.

I could of course fall back on:

parsers[1] = function(buf) {
 var ret = {type: "init"};
 ret.name = buf.readUTF8String();
 ret.capacity = buf.readUInt32();
 ret.port = buf.readUInt16();
 return ret;
}
asked May 8, 2015 at 11:53
5
  • 3
    This is the relevant part of the ES5 spec: 11.1.5 - Object initializer. Commented May 8, 2015 at 12:08
  • @joews: a bit cryptic, but I gather that they are evaluated in order. Thanks. Commented May 8, 2015 at 13:44
  • @joews he wants a guarantee, not an academic guess Commented May 8, 2015 at 14:33
  • A guess? If there is an answer, it's in that document. I can't decipher that section so I posted a link in the help it will help somebody else answer the question. Commented May 8, 2015 at 14:41
  • 2
    If you're writing code that expects function/method calls that reside in the value side of object initializers to be called in a specific order (because of side effects), then I'd suggest that you stop doing that and code specific line by line steps in the desired sequence. Why write code that is as difficult as this is to know if all Javascript implementations behave exactly the same way? If order is important and it isn't 100% obvious that all implementations do what you want with the initializers, then be safe and code the order yourself. Commented May 8, 2015 at 16:35

2 Answers 2

3

Thanks to @joews' comment, I can answer my own question.

From the link 11.1.5 Object initializer:

Syntax

ObjectLiteral :

  • { }

  • { PropertyNameAndValueList }

  • { PropertyNameAndValueList , }

PropertyNameAndValueList :

  • PropertyAssignment

  • PropertyNameAndValueList , PropertyAssignment

In short, the object constructor takes as arguments either nothing, a list of initialization values, or a list of initialization values followed by a comma.

That list of initialization value is composed by a PropertyAssignment or a list of initialization values followed by a PropertyAssignment, meaning basically a list of PropertyAssignment by recursion.

Now the question is in the last PropertyNameAndValueList , PropertyAssignment, is there a specific order in which both components are evaluated?

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.

  3. ...

The order will be guaranteed if 2. is sure to follow 1..

From 5.2 Algorithm conventions:

The specification often uses a numbered list to specify steps in an algorithm. These algorithms are used to precisely specify the required semantics of ECMAScript language constructs. The algorithms are not intended to imply the use of any specific implementation technique. In practice, there may be more efficient algorithms available to implement a given feature.

...

For clarity of expression, algorithm steps may be subdivided into sequential substeps.

So, the expected initialization order is element after element, from what I can gather.

answered May 8, 2015 at 16:04
0
0

As in many other languages, I would not rely on any "order" of properties of an object nor on the way they are initialized or values are assigned to.

If an "external" order is necessary, I would try to achieve that with a kind of mapping of these properties.

answered May 8, 2015 at 12:02
4
  • 1
    This does not have anything to do with the order of the actual properties when within the object, but rather whether when initialised that the order of expressions is respected (important due to potential side effects of function calls). Commented May 8, 2015 at 12:15
  • And if you read Axel's answer that's what he says. Your response reads like you made it to just before the work "nor". Commented May 8, 2015 at 13:29
  • Are you saying that you personally wouldn't assume that the properties are not assigned in order, or that you have evidence that they aren't? If the latter can you provide some citation? Thanks! Commented May 8, 2015 at 14:47
  • 2
    @RGraham I wouldn't rely on it and for clearness of code I would not work with any kind of "hidden feature". I recommend to implement any necessary order by a visible application logic. Maybe it's hard to draw a line on where to stop since at the end you have to rely on something, remains code clearness. Imagine in half a year someone is changing the behavior of the buffer functions - ... Commented May 8, 2015 at 15:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.