Consider Sec. 11.2 of ECMA-262.
Syntax
MemberExpression :
PrimaryExpression
FunctionExpression
MemberExpression [ Expression ]
MemberExpression . IdentifierName
new MemberExpression Arguments
NewExpression :
MemberExpression
new NewExpression
CallExpression :
MemberExpression Arguments
CallExpression Arguments
CallExpression [ Expression ]
CallExpression . IdentifierName
Arguments :
( )
( ArgumentList )
ArgumentList :
AssignmentExpression
ArgumentList , AssignmentExpression
LeftHandSideExpression :
NewExpression
CallExpression
The PrimaryExpression is the following
PrimaryExpression :
this
Identifier
Literal
ArrayLiteral
ObjectLiteral
( Expression )
The first question is:
What ( Expression )
does mean in the PrimaryExpression
defenition?
The {prop: 'prop'}
is ObjectLiteral
. Thus {prop: 'prop'}()
is CallExpression
. I'm trying to check this with JSFIDDLE but I have
[20:16:12.347] SyntaxError: syntax error @ http://fiddle.jshell.net/_display/:21
The second question:
Why this error was caused? I think that {prop: 'prop'}()
is correct line and I'm excepted that the error will be kind of {prop: 'prop'} is not a function
.
UPD: I'm using firefox 25.0.1
First question:
( Expression )
simply means a (
, an Expression
, and then a )
.
Second question:
{prop: 'prop'}()
Is being parsed as:
// a block
{
// syntax error
prop: 'prop'
}
// syntax error
()
You could add parens, and then you would get the expected error:
({prop: 'prop'}())
This also works since a block is not valid there:
var obj = {prop: 'prop'}
obj()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With