views:

683

answers:

5

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
})();
A: 

Is there a difference? Are you seeing a difference in execution at all? This is a super interesting question.

Shaun F
A: 

wild guess : there is no difference.

Jobo
+3  A: 

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
No. Without the parentheses, a single statement containing a function expression will give a syntax error.
Tim Down
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
@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
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
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
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
+2  A: 
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
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.
Pumbaa80
+14  A: 

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

wrumsby