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
})();
4
  • can you give a bit more context? Commented Jan 8, 2009 at 4: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. Commented Jan 8, 2009 at 4:32
  • Yes, Javascript syntax highlight gets disabled for the entire code block between the parenthesis. Commented Jan 8, 2009 at 4: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. Commented Jan 27, 2010 at 19:32

3 Answers 3

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,

Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

No. Without the parentheses, a single statement containing a function expression will give a syntax error.
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: 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.
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.
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.
|
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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.