38

Possible Duplicate:
Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript?

This is something I haven't quite figured out yet, but I have been using function(){}() just because my VIM syntax highlight screws up if I add the parenthesis, although I've seen (function(){})() around many times, maybe its an IE thing?

edit:

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

var singleton = (function() {
    // code
})();
Community
  • 1
  • 1
Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
  • can you give a bit more context? – Jesse Pepper Jan 08 '09 at 04:08
  • Why can't VIM handle the parens? I'm surprised there are so many people using it if it can't handle that case. – recursive Jan 08 '09 at 04:32
  • Yes, Javascript syntax highlight gets disabled for the entire code block between the parenthesis. – Luca Matteis Jan 08 '09 at 04:34
  • 7
    In the example shown above, there is no difference, because the parser is expecting an expression after the =. When you are not assigning the output, you need the parens to disambiguate the function statement from a function expression. The parens put the parser into an expression context. – Sean McMillan Jan 27 '10 at 19:32

3 Answers3

45

Peter Michaux discusses the difference in An Important Pair of Parens.

Basically the parentheses are a convention to denote that an immediately invoked function expression is following, not a plain function. Especially if the function body is lengthy, this reduces surprises,

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Walter Rumsby
  • 7,435
  • 5
  • 41
  • 36
10

The extra set of parentheses makes it clearer that you are constructing a function and then calling it. It's a coding style thing, not a functionality thing.

Henk
  • 3,193
  • 2
  • 17
  • 12
  • 3
    No. Without the parentheses, a single statement containing a function expression will give a syntax error. – Tim Down Jan 27 '10 at 00:55
  • 2
    The 2 examples provided by OP are all function expressions, and would not have syntax error even with that parenthesis removed. Only function declaration would. – bryantsai Jan 28 '10 at 06:41
  • @bryantsai: you have a point, in that the OP's example (added later ) uses a function expression as part of an assignment and therefore doesn't require the parentheses, but the question is more general and this answer is misleading. Also, I'm confused about what you're suggesting in your reference to function declarations. – Tim Down Jan 28 '10 at 09:53
  • 1
    I guess the title is a little inconsistent, regarding OP's 2 examples. However, looking at question content as well as the top 2 answers, I feel they are just right. Also, what I mean by mentioning function declaration is that parenthesis only matters when used on function declaration. A function declaration cannot be called immediately. But if "grouped" inside parenthesis, function declaration becomes a function expression and so can be called immediately. What I meant was that both OP's 2 examples are all function expressions and hence with or without parenthesis doesn't make a difference. – bryantsai Jan 28 '10 at 11:00
  • OK, I see. My point still stands though: a statement solely consisting of a function expression (e.g. `function() { alert("1"); }`) is a syntax error. – Tim Down Jan 28 '10 at 15:27
  • No one is disagreeing with you in that respect. Indeed, no-one said anything about statements at all, with the OP using them very explicitly as expressions. – Henk Feb 02 '10 at 21:08
4
function(){}();

doesn't work in most of browsers. You should use parenthesis around the function in order to execute it

(function(){})();

then browser will know that last parenthesis should be applied to all the expression

function(){}

UPD: If you don't use parenthesis, the brower could misunderstand you. If you just call the function and dismiss the result

function() {
    alert(1);
}();

then ff and ie will throw error

x-yuri
  • 16,722
  • 15
  • 114
  • 161
  • 5
    function(){}(); does work in all browsers if it is a function expression. Particularly, var foo = function(){}(); works. Read http://yura.thinkweb2.com/named-function-expressions/#expr-vs-decl for more information. – user123444555621 Sep 23 '09 at 12:39