2

I've seen two ways...the first makes most sense to me.

Self execution parentheses are placed directly after the function brackets. All is included between parentheses to make it a function expression. Reference here

( function () {
    // ... all vars and functions are in this scope only
    // still maintains access to all globals
} () );

and this style, where self-execution parentheses are place after the parentheses which create the function expression. Reference here

var Var = ( function ( window, undefined ) 
{

} )();

I'm not sure if the var makes a difference in the syntax...?

MrTux
  • 32,350
  • 30
  • 109
  • 146
CS_2013
  • 1,158
  • 3
  • 13
  • 24
  • You seem to be talking about [immediate functions](http://stackoverflow.com/questions/939386/immediate-function-invocation-syntax), not really the module pattern. If so, there's another approach, `!function () { /* do stuff */ }();` But what was the question? – kojiro May 19 '12 at 19:44
  • Both examples above are of the module pattern ( with references ). I want to know why the parentheses are in different places for each. – CS_2013 May 19 '12 at 19:47

3 Answers3

4

The answer to your question is in the link about immediate functions in my first comment. Essentially, if you just want an immediate function, as long as the function declaration itself is wrapped in parentheses, it doesn't matter whether the calling parens follow the curly braces or follow the wrapping parentheses. However, if you don't want the wrapping parens, then the assignment to Var becomes relevant, as this will work:

var Var = function () {
    // do stuff; return stuff;
}();

But this will not:

function () {
    // do stuff;
}();

because it is only a function declaration followed by an unexpected pair of parentheses (a syntax error).

Community
  • 1
  • 1
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • An immediate function not assigned to a var requires wrapping in parentheses, while an immediate function assigned to a var does not require wrapping parentheses...Cool. Can you relate this to my question about the module pattern ....are you implying / suggesting that the second example I posted above is over-kill that it does not need the wrapping parentheses? – CS_2013 May 19 '12 at 19:57
  • Strictly speaking it does not require the wrapping parens, but I don't consider them overkill. Consider that someone reading your code might think you're assigning the *function* to `Var`, to be executed later. By wrapping it in parens you make it clear that something else is going on. – kojiro May 19 '12 at 20:00
  • O.K. Seems like in cases were you can do one of two ways - one way should be the default to avoid confusion. I am always going to put my parentheses immediately after the function brackets as this makes the most sense to me...to denote an immediate function. – CS_2013 May 19 '12 at 20:06
3

There's no difference between:

(function () {})();

and

(function () {}());

Douglas Crockford advocates for the second though (so JSLint will complain about the first style). He says the first is "counter-productive", whatever that means... I prefer the first.

Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • It is an 'operation' on the function...it makes it immediate...makes sense to put it close to what it works on...my one cent. – CS_2013 May 19 '12 at 20:19
  • I'm still not convinced, whether to completely drop the outer parens. Works fine for me. – weiglt Sep 12 '13 at 07:31
0

It's actually 2 different patterns. The 2nd patterns is called the revealing module pattern as in it reveals some of the locals into the global namespace.

Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68
  • One things that eludes me is the fact that any function, can be written to return another function. This allows one to write a function with private/public variables and functions. The only thing I see gained by using the the module pattern or revealing module pattern are global imports. I use it by default b.c. everyone else does...but it seems a function would do the same thing essentially. – CS_2013 May 19 '12 at 20:15