Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript anonymous function immediate invocation/execution (expression vs. declaration) [duplicate]

Possible Duplicates:
What is the difference between a function expression vs declaration in JavaScript?
Explain JavaScript's encapsulated anonymous function syntax

Why this:

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

and this:

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

works, while this:

function () {     //code }(); 

does not? It looks exactly the same - anonymous function defined, and immediately called. Can someone make a quotation from the JavaScript/ECMAScript standard which explains that?

UPDATE: Thanks for the answers everyone! So it's about function expression vs. function declaration. See this Stack Overflow answer, ECMAScript standard section 13, and this great article: Named function expressions demystified.

To recap answers:

  1. The first snippet is interpreted as an expression because the grouping operator, (), is applied - see ECMAScript standard section 11.1.6.

  2. In the second snippet, the function is interpreted as an expression because it's on the right-hand part of assignment operator, =.

  3. The third snippet doesn't have anything which allows the interpreter to read the function as an expression, so it's considered a declaration, which is invalid without an identifier (Gecko lets it pass however, but it chokes on following () grouping operator (as it thinks) applied to nothing).

like image 404
lxa Avatar asked Jul 16 '11 17:07

lxa


People also ask

What is the difference between a function declaration vs a function expression?

The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined.

Should I use function expression or declaration?

In short, use function declarations when you want to create a function on the global scope and make it available throughout your code. Use function expressions to limit where the function is available, keep your global scope light, and maintain clean syntax.

Does declared function execute immediately?

Function expressions are evaluated during execution. So you can't execute function declarations immediately because even variables don't exist yet and other code that the function might depend on hasn't been executed either.

What is the point of an IIFE?

An Immediately-invoked Function Expression (IIFE for friends) is a way to execute functions immediately, as soon as they are created. IIFEs are very useful because they don't pollute the global object, and they are a simple way to isolate variables declarations.


1 Answers

The first two cases show function expressions, and can appear anywhere an expression like (1+1 or x*f(4)) would appear. Just like how 1+1, evaluates to 2, these expressions evaluate to a corresponding function.


The third case is a function declation statement, and can appear anywhere you can have other statements (like an if or while statement).

There is not much point in trying to declare an anonymous function via a Funcion declaration statements, since otherwise noone would get a reference to the function afterwards.


The reason you need the opening ( or the var x = like in the first two cases is that they force next bit to be parsed in an expression context. (just think how you cant do var x = if ..., for example). If you just put the function as the first thing it will be parsed as the declaration statement you don't want.

like image 185
hugomg Avatar answered Oct 14 '22 11:10

hugomg