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;
}
-
3This is the relevant part of the ES5 spec: 11.1.5 - Object initializer.joews– joews05/08/2015 12:08:17Commented May 8, 2015 at 12:08
-
@joews: a bit cryptic, but I gather that they are evaluated in order. Thanks.coyotte508– coyotte50805/08/2015 13:44:51Commented May 8, 2015 at 13:44
-
@joews he wants a guarantee, not an academic guessAxel Amthor– Axel Amthor05/08/2015 14:33:22Commented 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.joews– joews05/08/2015 14:41:03Commented May 8, 2015 at 14:41
-
2If 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.jfriend00– jfriend0005/08/2015 16:35:53Commented May 8, 2015 at 16:35
2 Answers 2
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:
Let
obj
be the result of evaluating PropertyNameAndValueList.Let
propId
be the result of evaluating PropertyAssignment....
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.
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.
-
1This 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).Qantas 94 Heavy– Qantas 94 Heavy05/08/2015 12:15:37Commented 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".Matthew Whited– Matthew Whited05/08/2015 13:29:09Commented 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!CodingIntrigue– CodingIntrigue05/08/2015 14:47:34Commented 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 - ...Axel Amthor– Axel Amthor05/08/2015 15:20:21Commented May 8, 2015 at 15:20