Difference between (function(){})(); and function(){}(); [duplicate]
Asked Answered
B

3

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
})();
Bertrando answered 8/1, 2009 at 4:3 Comment(4)
can you give a bit more context?Idolah
Why can't VIM handle the parens? I'm surprised there are so many people using it if it can't handle that case.Tirol
Yes, Javascript syntax highlight gets disabled for the entire code block between the parenthesis.Bertrando
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.Fractocumulus
P
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,

Papyrology answered 8/1, 2009 at 4:3 Comment(0)
E
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.

Espresso answered 8/1, 2009 at 4:21 Comment(6)
No. Without the parentheses, a single statement containing a function expression will give a syntax error.Quixotism
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.Kev
@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.Quixotism
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.Kev
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.Quixotism
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.Espresso
A
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

Anh answered 8/1, 2009 at 4:23 Comment(1)
function(){}(); does work in all browsers if it is a function expression. Particularly, var foo = function(){}(); works. Read yura.thinkweb2.com/named-function-expressions/#expr-vs-decl for more information.Munger

© 2022 - 2024 — McMap. All rights reserved.