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…?
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
Varbecomes relevant, as this will work:But this will not:
because it is only a function declaration followed by an unexpected pair of parentheses (a syntax error).