@DanMan...it is the same exact thing. Crockford just happens to like the "calling" parenthesis on the "inside". It's just a personal preference (I actually agree with Crockford's preference on this one).
–
David MurdochDec 29 '10 at 1:46

I'm not sure how you call this a side-effect...if you wanted to execute the code immediately (and not a closure) why wrap it in a function in the first place? It's a direct and intentional effect.
–
Nick Craver♦Sep 15 '10 at 18:02

1

@Nick Craver: see edit. I meant an effect, but the intended effect.
–
palswimSep 15 '10 at 18:07

7

@Nick: a closure is a possible side effect. A function is not a closure. A function without a name is called an anonymous function, mistakenly called a closure by those not familiar with functional languages. In javascript things declared in braces (..) are expressions, so that is an anonymous function expression. All expressions return something. In the case of function expressions it returns a reference to a function. None of the things above are called closures in the traditional sense. A closure is a variable shared between functions, not the function itself.
–
slebetmanSep 15 '10 at 18:36

1

@slebetman - You didn't read the comments above, or my updated answer from 20 minutes ago, I clarified exactly this: "It's only a closure when something inside that scope is exposed to an outer scope, which is usually the case, but I can't be sure for your example without seeing more code. If nothing is exposed then no closure's created...otherwise it's just an anonymous function executing immediately."
–
Nick Craver♦Sep 15 '10 at 18:37

2

@Nick: even then it's not a closure, it's an anonymous function. The two concepts are separate though closures depend on functions.
–
slebetmanSep 15 '10 at 18:44

To clarify a bit for the comments below, most of the time it's creating a closure, it keeps your variables scoped to that local closure, as to not create global variables, it both keeps things clean and avoids any potential unwanted changes to those variables.

It's only a creating closure when something inside that scope is exposed to an outer scope, which is usually the case, but I can't be sure for your example without seeing more code. If nothing is exposed then no closure's created...otherwise it's just an anonymous function executing immediately.

The })(); format at the end, as opposed to }); is actually calling that closure to execute immediately, with no parameters. If you had something in it, for example })(something); then that something would be passed as the first argument here: (function(somethingParam){.

Strictly speaking a closure is a side-effect of the function. This isn't a closure, it's a function. In fact, there probably isn't even a closure being created in this case, since the function's contents aren't accessible from outside it. See jibbering.com/faq/notes/closures
–
Jani HartikainenSep 15 '10 at 17:58

2

@Jani - That's what a closure does... it's specifically for (in this case) hiding the contents from the outside, while having them accessible to anything inside. Even the link you provided gives this exact definition: "The simple explanation of a Closure is that ECMAScript allows inner functions; function definitions and function expressions that are inside the function bodes of other functions. And that those inner functions are allowed access to all of the local variables, parameters and declared inner functions within their outer function(s)."
–
Nick Craver♦Sep 15 '10 at 18:01

3

@Nick, the line you quote is referring to the way the Identifier Resolution process works, not specifically with the formation of a closure, continuing with the quote: "A closure is formed when one of those inner functions is made accessible outside of the function in which it was contained, so that it may be executed after the outer function has returned." So if no inner function is made available to the outside, a closure is not formed -what @Jani points out-, IMO the term closure is sometimes overused nowadays.
–
CMSSep 15 '10 at 18:08

3

@Nick, yeah, the example is incomplete. Yes, you almost always expose something to the outside, in the case of a function (or an object containing a property that references to a local function) a closure is formed. Thanks for the clarification.
–
CMSSep 15 '10 at 18:19

2

@Nick: A function is not a closure. Don't get your terminologies mixed up. A closure is the variable shared by functions. A function can create a closure but it in itself is not a closure. It's like calling an oven a cake. A cake is not an oven but an oven may be used to bake a cake.
–
slebetmanSep 15 '10 at 18:42

If his post is too long for you here is my summary (I still recommend reading it as this summary leaves out a lot):

If you want a named function to be self executing/invoking it would should look like this:

// Hello, my name is "foo". I am a named function.
// When I am invoked I invoke my self when I am invoked.
function foo(){
foo();
}

If you want an anonymous function to be self executing/invoking it should look like this:

// Hello, I have no name...
// (though I am assigned to the variable "foo" it's not who I am).
// When I am invoked I invoke my self when I am invoked.
// In ECMAScript 5 I no longer work. :-(
var foo = function(){
arguments.callee();
};

If you want an anonymous function to be immediately executed/invoked it should look like this:

The other answers are correct; what you are asking about is commonly referred to as a "self invoking anonymous function."
However, that terminology doesn't accurately reflect what is really happening; "Immediately-Invoked Function Expression" (aka "iffy", for short) seems like a more appropriate term.

I don't recommend using those "shortcuts" since most devs don't know about it and I'm not sure about browser compatibility. Test it cross-browser and if it works everywhere you could always do this: !(function(){}()); so you still get to use the nifty ! and the widely known "Immediately-Invoked Function Expression".
–
David MurdochJan 5 '11 at 20:19

The module pattern is more specific than this. It use a "closure" as a way to provide private methods and variables to an object or function that is returned at the initial (immediate) invocation.
–
David MurdochDec 29 '10 at 1:52

So it only counts as a module if it returns an object (possibly with private state hidden in local variables)? Too bad.
–
Sean McMillanJan 4 '11 at 19:56

It's called an immediately-invoked function expression, in short: IIFE. It defines a function in an expression, which is then executed on its own (without assigning the function to any identifier). It sometimes is also called immediately executed function expression (IEFE).

Before Ben Alman wrote his blog post on them, they were also known as self-invoking (anonymous) functions, a term which became uncommon since then. It was technically imprecise, hinting at a recursive invocation which does not actually happen.