1

I would like to ask that what the main difference between

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

and

function() {
 ....
 ....
}();
Huangism
  • 16,278
  • 7
  • 48
  • 74
user3733648
  • 1,323
  • 1
  • 10
  • 25
  • 4
    The first one is valid JS, the second one is a syntax error. However, assuming an expression context in both cases, the differences between them is the same as between `5` and `(5)` (none). – Felix Kling Mar 25 '15 at 14:37
  • The first is an anonymous closure, commonly used in the [module pattern](http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html) – agconti Mar 25 '15 at 14:39
  • second one should be `(function() {}());` – epascarello Mar 25 '15 at 14:46
  • possible duplicate of [Location of parenthesis for auto-executing anonymous JavaScript functions?](http://stackoverflow.com/q/3384504/1048572) Is that what you were after? – Bergi Mar 25 '15 at 15:23

3 Answers3

1

The first one is a Self Invoking Function.

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

The function will be invoked by itself. It's mostly used for wrapping the code inside to prevent the variables from polluting the global namespace.You can notice it in popular libraries such as jQuery.

Another use case of it is:

As javascript is function scoped language when compared to other languages which are usually block scoped. If you want to create local variables inside function you can use IIFE for that purpose as well.

The second one is actually a syntax error. But it can be like

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

It's another way of representing the self invoking function.

mohamedrias
  • 18,326
  • 2
  • 38
  • 47
  • *"It forms a closure which prevents the inner variables from polluting the global namespace."* Every function is a closure. The variables are not polluting the global namespace because functions create scope. The closure part is irrelevant here. – Felix Kling Mar 25 '15 at 14:43
0

The first one is immediately invoked function, the second one is invalid JS, because when the parser encounters the function keyword, it find it as a function declaration, but not as a function expression.

For immediately invoked function best way is

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

or

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

But you can also prefixing the function with a unary operator, it will be save a byte

!function(){ ... }();
~function(){ ... }();
-function(){ ... }();
+function(){ ... }();
marsh
  • 1,431
  • 1
  • 13
  • 19
0

They aren't really function signatures, they are Immediately-invoked function expressions (IIFE), and usually these are the two forms:

(function(){
/* ... */
})();

and

(function(){
/* ... */
}());

the parenthesis cause the function to be treated as a function expression. without them, the function will be treated as a function declaration

Nick
  • 158
  • 1
  • 7
  • *"the parens are necessary because it causes the interpreter to interpret the function as either a function expression or function declaration"* The *absence* of parenthesis would cause the interpreter to treat them like function declarations. *With* parenthesis they will always be evaluated as function expressions. – Felix Kling Mar 25 '15 at 14:45
  • Yeah that was phrased kind of weird. I'll update it, thanks :) – Nick Mar 25 '15 at 15:12