JavaScript anonymous call / execute function (expression versus declaration)

Possible duplicates:
What is the difference between function expression and declaration in JavaScript? Explain JavaScript Anonymous Function Syntax

Why is this:

(function () { //code }()); 

and this:

 var f = function () { //code }(); 

It works, and this:

 function () { //code }(); 

no? It looks exactly the same - an anonymous function is defined and immediately called. Can someone make a quote from the JavaScript / ECMAScript standard that explains this?

UPDATE : Thanks for all the answers! So this is an expression of an expression of a function and declaration . See this answer, "Stack Overflow," the ECMAScript standard , and this great article: Named Functional Expressions, Demystified .

Repeat answers:

  • The first fragment is interpreted as an expression because the grouping operator () is used - see ECMAScript 11.1.6.

  • In the second fragment, the function is interpreted as an expression, because it is on the right side of the assignment operator, = .

  • In the third fragment there is nothing that allows the interpreter to read the function as an expression, so he considered an ad that is invalid without an identifier (Gecko allows it to pass, but it suffocates after () the grouping operator (as it thinks) does not apply to anything).

+54
javascript anonymous-function
Jul 16 2018-11-17T00:
source share
4 answers

The first two cases show the expressions of functions and can appear anywhere where an expression of the type ( 1+1 or x*f(4) ) appears. Just as 1+1 evaluates to 2 , these expressions evaluate the corresponding function.




The third case is an expression of an expression

+20
Jul 16 '11 at 18:09
source share

The first two are something called a function expression, meaning it is built-in and interpreted as JS code.

The third is a function declaration and is interpreted when compiling the code. Since it is interpreted during compilation, you cannot run it immediately, since none of the other codes around it are running.

To show an example:

 // foo == undefined // bar == function function bar(){ .. } var foo = function(){ ... } // foo == function // bar == function 

Simply put, anytime you have a word function without anything preceding it, it is an ad. At any time, something precedes him, this expression.

+4
Jul 16 2018-11-17T00:
source share

Anonymous functions are well explained in stack overflow question Why do you need to call an anonymous function on the same line? .

0
Jul 16 '11 at 17:44
source share

Here's a simple way to think about it: if function is the first keyword in a string, the parser interprets the rest of the string as a function declaration. In other words, he will think that you are trying to write something like this, as if you forgot to name your function:

 function foo(){ // code } 

A way around this is to either wrap the entire function inside some partners or make it part of the variable assignment. In any case, you put the function back in the string and let the parser recognize that you are not writing a function declaration.

It seems trivial to me to let function appear at the beginning of the line and still distinguish between function expressions and function declarations, but I think it wasn’t so trivial when JavaScript was first developed.

0
Jul 16 2018-11-17T00:
source share



All Articles