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?
-
6One has more brackets, obviously.Ryan Kempt– Ryan Kempt2013年06月27日 01:06:09 +00:00Commented Jun 27, 2013 at 1:06
-
2It's a style decision. Crockford justifies using parenthesis 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– Felix Kling2013年06月27日 01:13:54 +00:00Commented Jun 27, 2013 at 1:13
-
Slightly related (Crockford talking about parentheses positioning): youtube.com/watch?v=eGArABpLy0kFelix Kling– Felix Kling2013年06月27日 01:31:25 +00:00Commented Jun 27, 2013 at 1:31
5 Answers 5
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.
3 Comments
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:
1 Comment
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.
Comments
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
Comments
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() {})();
3 Comments
(function(){...do something...}()); and var myfunc = function(){}; can be assumed to run in the same scope. Or I'm not understanding your answer correctly.(); 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();. The parentheses return a value and either you save it in a variable or not.