Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between (function() {...}()); and (function() {...})();? [duplicate]

Tags:

Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?

Sometimes I see:

(function() { ... }());  

and sometimes I see:

(function() { ... })();  

I see both forms with and without arguments. They both execute the anonymous function.

Is there a difference between the two forms? Are there any compelling reasons to use one form over the other?

like image 314
Peter Ajtai Avatar asked Sep 23 '10 22:09

Peter Ajtai


People also ask

Whats the difference between this function and this function ()?

this. functionName reads the value of property functionName of the object this . this. functionName() reads the value of property functionName of the object this and tries to call it as a function.

What's the difference between a normal function and arrow function?

In regular function, arguments will give you list of parameter passed in function, In arrow function arguments is not defined. In regular function, you always have to return any value, but in Arrow function you can skip return keyword and write in single line. In arrow function parameters should be unique.

What is a JavaScript method vs function?

Use this article as a reference sheet for JavaScript methods and functions. Function — a set of instructions that perform a task. Method — a set of instructions that are associated with an object.


1 Answers

There is no practical difference in those two forms, but from a grammatical point of view the difference between the two is that The Grouping Operator - the parentheses - will hold in the first example a CallExpression, that includes the FunctionExpression:

                CallExpression                 |         |        FunctionExpression |                 |         |                 V         V     (function() {       }());     ^                      ^     |--PrimaryExpression --|  

In the second example, we have first a whole CallExpression, that holds the FunctionExpression:

           PrimaryExpression                 |          FunctionExpression                 |                 V     (function() {       })();     ^                      ^     |--  CallExpression  --|  
like image 124
Christian C. Salvadó Avatar answered Sep 28 '22 06:09

Christian C. Salvadó