6

Any difference between

var myfunc = (function () { return function () { ... } }());

and

var myfunc = function () { return function () { ... } }();

Is it just a matter of style or is there more to the surrounding () in the first form?

Alex Wayne
  • 145,435
  • 42
  • 271
  • 302
user2526241
  • 619
  • 5
  • 10
  • 6
    One has more brackets, obviously. – Ryan Kempt Jun 27 '13 at 01:06
  • 2
    It's a style decision. [Crockford justifies using parenthesis](http://javascript.crockford.com/code.html) with *"When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself."*. Personally I agree with him in this point. – Felix Kling Jun 27 '13 at 01:13
  • Slightly related (Crockford talking about parentheses positioning): http://www.youtube.com/watch?v=eGArABpLy0k – Felix Kling Jun 27 '13 at 01:31

5 Answers5

6

Nope. Or at least not in your example.

The outer parens only matter when the function keyword would be the first token in a statement.

// cool
var foo = function(){}();
var foo = (function(){}());

// also cool
(function(){}());

// not cool, syntax error
// parsed as function statement, expects function name which is missing
function(){}();

// also not cool, syntax error
// declares a function, but can't be executed immediately
function foo(){}();

When function is the first token in a statement, it's a function declaration (think named function), which behaves slightly differently than function in all other contexts. That leading paren forces the parses to treat it like a function expression (think anonymous function) instead, which allows immediate execution.

See: What is the difference between a function expression vs declaration in JavaScript?

If you start the line or statement with something else, like variable declaration, it technically doesn't matter at all.

Community
  • 1
  • 1
Alex Wayne
  • 145,435
  • 42
  • 271
  • 302
  • You are not saying that every function expression is an anonymous function, are you? – Felix Kling Jun 27 '13 at 01:15
  • @FelixKling Close enough. There's a lot of depth and subtlety here. My answer does not explore it all. I merely meant that fn expressions are closer to "anonymous" in nature than fn declarations. Let's save explaining the difference between named and unnamed function expressions for a different question. – Alex Wayne Jun 27 '13 at 01:18
  • @FelixKling - Which is part of why Ben Alman is so obsessed about calling them [IIFE](http://benalman.com/news/2010/11/immediately-invoked-function-expression/)s. – Joseph Silber Jun 27 '13 at 01:38
1

No difference, though Crockford advises the use of the former, to ascertain that it's being treated as a function expression.

For more info, read these:

Joseph Silber
  • 193,614
  • 53
  • 339
  • 276
  • Thanks! That's where I saw it used, in his JavaScript: The Good Parts book. I thought I was missing something. I guess it's more readable, you are not quick to assume the "1st" function is being assigned to the variable. – user2526241 Jun 27 '13 at 01:06
0

no difference, it is just a matter of style. Both are executed functions and they return whatever they return in the variable and they are returning a function. check this out

Community
  • 1
  • 1
CME64
  • 1,643
  • 12
  • 24
0

There's no technical difference, It's just an idiomatic style used for readability to signal at the start that it's a self-invoking function.

mythz
  • 134,801
  • 25
  • 234
  • 373
0

the purpose is to avoid the global scope, adding the var myfunc = in front of (function (){ return function(){}}()); essentially defeats the purpose

compare the two

(function(){...do something...}());

this function is ran in a local scope, which is inside the parentheses.

var myfunc = function(){};

this function is ran on the global scope. why not this way, because you can get your self into naming conflicts with other methods and var as well as plugin's named var and it can hinder your applications performance with the var being saved on the global scope.

I perfer

(function() {})();
Jay Harris
  • 4,033
  • 15
  • 21
  • *"this function is ran in a local scope, which is inside the parentheses."* I think you are confusing some things here. Parenthesis don't create scope, only functions do. Immediately invoked function expressions (IIFE) are used to create local scope and "hide" variables from the scope the function is executed in. But both statements `(function(){...do something...}());` and `var myfunc = function(){};` can be assumed to run in the same scope. Or I'm not understanding your answer correctly. – Felix Kling Jun 27 '13 at 01:19
  • well any variable initialized inside the `();` is ran and then deleted by the gc, if a `var` i.e. `var myfunc` is declared outside of the `()` it is ran and stored in the global scope. your right they both run on the global scope, the difference is one being stored after running and the other being deleted by the gc – Jay Harris Jun 27 '13 at 01:25
  • That sounds more correct than what is written in the answer. Though you cannot initialize variables inside `();`. The parentheses *return* a value and either you save it in a variable or not. – Felix Kling Jun 27 '13 at 01:30