On Fri, 31 Dec 2010 14:46:37 +0100, Dmitry A. Soshnikov <[email protected]> wrote:

So, why does this code produces a SyntaxError:

function () {
   ...
}()
...
Try to provide as much complete explanation as possible. Also try to answer without cheating ;)

Because an ExpressionStatement may not start with the token "function", and a FunctionDeclaration may not omit the function name identifier, so there is no
matching rule in the syntax.

With the variable name, the first part does match a FunctionDeclaration, but there is no rule that allows an opening parenthesis after a function declaration.

Or more indirectly: The syntax generally requires only one token of look-ahead to identify the production to use. It disallows an ExpressionStatment from starting with "function" (or "{") to avoid ambiguity with FunctionDeclaration (or a statement block). That means that the above cannot possibly be detected as an immediate function
call, but only as a FunctionDeclaration with garbage at the end.

/L

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to