When trying to access the property a
of the object {}
{}.a
I get the error
SyntaxError: Unexpected token .
With parens all is fine:
({}).a
Why do I get an error in the fist place? Is there ambiguity?
When trying to access the property a
of the object {}
{}.a
I get the error
SyntaxError: Unexpected token .
With parens all is fine:
({}).a
Why do I get an error in the fist place? Is there ambiguity?
The curly braces are interpreted as a block statement, not as an object literal. You cannot begin an expression statement with a left curly brace.
The specification states:
NOTE An ExpressionStatement cannot start with an opening curly brace because that might make it ambiguous with a Block. Also, an ExpressionStatement cannot start with the
function
keyword because that might make it ambiguous with a FunctionDeclaration.
the {} are there to build the object. usually you first assign the new object to a variable.
var o = {
a: "b"
};
console.log(o.a);
but this is also possible:
console.log({
a: "b"
}.a);