Location of parenthesis for auto-executing anonymous JavaScript functions?
Asked Answered
L

4

113

I was recently comparing the current version of json2.js with the version I had in my project and noticed a difference in how the function expression was created and self executed.

The code used to wrap an anonymous function in parenthesis and then execute it,

(function () {
  // code here
})();

but now it wraps the auto-executed function in parenthesis.

(function () {
  // code here
}());

There is a comment by CMS in the accepted answer of Explain JavaScript’s encapsulated anonymous function syntax that “both: (function(){})(); and (function(){}()); are valid.”

I was wondering what the difference is? Does the former take up memory by leaving around a global, anonymous function? Where should the parenthesis be located?

Lusatian answered 2/8, 2010 at 1:39 Comment(3)
See also Difference between (function(){})(); and function(){}(); and Is there a difference between (function() {…}()); and (function() {…})();?Earnest
Related: Immediate function invocation syntax (in JSLint)Earnest
Also read about the purpose of this construct, or check a (technical) explanation (also here). For why the parenthesis are necessary, see this question.Earnest
S
67

They're virtually the same.

The first wraps parentheses around a function to make it a valid expression and invokes it. The result of the expression is undefined.

The second executes the function and the parentheses around the automatic invocation make it a valid expression. It also evaluates to undefined.

I don't think there's a "right" way of doing it, since the result of the expression is the same.

> function(){}()
SyntaxError: Unexpected token (
> (function(){})()
undefined
> (function(){return 'foo'})()
"foo"
> (function(){ return 'foo'}())
"foo"
Summand answered 2/8, 2010 at 1:49 Comment(9)
JSLint wants "(function(){}());". JSLint says, "Move the invocation into the parens that contain the function."Invent
Actually you are not limited to those two, you can use just about anything that makes the compiler realize the function is part of an expression and not a statement, such as +function(){}() or !function(){}().Besotted
@XP1: JSLint wants lots of things that are specific to Crockford's style rather than being substantive. This is one of them.Agleam
@T.J.Crowder. What would you recommend? jQuery uses the first style and Crockford uses the second.Jasmine
@ThorpeObazee: It genuinely doesn't matter, so do whatever you prefer. I'd recommend against some of the more outre ones (-function(){}();, !function(){}();, and basically any other operator just before function also work, but I'd stick to versions using parens). I see the first a lot more than I see the second, and it's my preference; it makes more sense to me as well, but that's subjective. FWIW: jsbin.com/ejaqowAgleam
@T.J.Crowder I have a feeling the V8 parser considers parens a hint that the function will be immediately executed, which is another reason to use them over the weirder options.Monaural
@Andrew: I doubt it, since other options can mean the same thing. The V8 parsing/compilation is fairly sophisticated these days. Heck, it's even a two-phase hotspot-style dohickey these days... :-)Agleam
Here is a detailed description why it's better to move the invocation inside: medium.com/airbnb-engineering/…Thi
Eager avaluation is possible: This set x instantly to the value instead of function; let x = (function(){return 123})()Azzieb
G
13

In that case it doesn't matter. You are invoking an expression that resolves to a function in the first definition, and defining and immediately invoking a function in the second example. They're similar because the function expression in the first example is just the function definition.

There are other more obviously useful cases for invoking expressions that resolve to functions:

(foo || bar)()
Generatrix answered 2/8, 2010 at 1:49 Comment(2)
For clarification to other readers (mainly because I didn't understand it at first myself :) ), foo and/or bar must already equal some function. (e.g. foo = function(){alert('hi');}. If neither are a function, an error is thrown.Broody
@AlexanderBird A further clarification - It will also throw an error if foo is "truthy" but not a function.Fatigued
S
11

There isn't any difference beyond the syntax.

Regarding your concerns about the second method of doing it:

Consider:

(function namedfunc () { ... }())

namedfunc will still not be in the global scope even though you provided the name. The same goes for anonymous functions. The only way to get it in that scope would be to assign it to a variable inside the parens.

((namedfunc = function namedfunc () { ... })())

The outer parens are unnecessary:

(namedfunc = function namedfunc () { ... })()

But you didn't want that global declaration anyways, did you?

So it it boils down to:

(function namedfunc () { ... })()

And you can reduce it even further: the name is unnecessary since it will never be used (unless your function is recursive.. and even then you could use arguments.callee)

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

That's the way I think about it (may be incorrect, I haven't read the ECMAScript specification yet). Hope it helps.

Sonyasoo answered 2/8, 2010 at 2:35 Comment(2)
Note that arguments.callee is deprecated since ES5 (and forbidden in strict mode).Ileum
"The outer parens are unnecessary:" - I think they prevent errors when files are concatenated, otherwise you'd need a ! or something.Encephaloma
A
-2

The difference just exist because Douglas Crockford doesn't like the first style for IIFEs! (seriuosly) As you can see in this video!!.

The only reason for the existence of the extra wrapping () {in both styles} is to help make that section of code Function Expression, because Function Declaration cannot be immediately called. Some scripts / minify-ers just use +, !, - & ~ instead of too parentheses. Like this:

+function() {  
    var foo = 'bar';  
}();

!function() {  
    var foo = 'bar';  
}();

-function() {  
    var foo = 'bar';  
}();

~function() {  
    var foo = 'bar';  
}();

And all these are exactly the same as your alternatives. Choosing among these cases is completely on your own & makes no difference. { The ones with () produce 1 Byte larger File ;-) }

Anglesite answered 24/2, 2017 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.