Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding terminology and notation JavaScript

Tags:

javascript

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

like image 798
St.Antario Avatar asked Oct 20 '22 18:10

St.Antario


1 Answers

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()
like image 61
tckmn Avatar answered Oct 24 '22 10:10

tckmn