Why is this function call incorrect in JavaScript?

I would like to create an anonymous function and then call it immediately.

1) This will result in a syntax error. Why?

function ()
{
    alert("hello");
}();

2) complete the function definition with () and it works.

(function ()
{
    alert("hello");
})();

3) or assign an anonymous function to a variable. He works.

var dummy = function()
{
    alert("hello");
}();

Why does the first method not work?

+5
source share
1 answer

ECMAScript Language Specification , Section 12.4, says:

Cannot start ExpressionStatement application with keyword functionbecause it can make it ambiguous with FunctionDeclaration.

, 1 , . ( ExpressionStatements), , .

+12

All Articles