tags:

views:

1655

answers:

8

In javascript, when would you want to use this:

(function(){
    //Bunch of code...
})();

over this:

//Bunch of code...
+9  A: 

Namespacing. JavaScript's scopes are function-level.

Christoph
A: 

IIRC it allows you to create private properties and methods.

Ólafur Waage
+14  A: 

Its all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of how variables are named in other blocks of javascript code.

Ken Browning
+1  A: 

Since functions in Javascript are first-class object, by defining it that way, it effectively defines a "class" much like C++ or C#.

That function can define local variables, and have functions within it. The internal functions (effectively instance methods) will have access to the local variables (effectively instance variables), but they will be isolated from the rest of the script.

James Curran
+2  A: 

Scope isolation, maybe. So that the variables inside the function declaration don't pollute the outer namespace.

Of course, on half the JS implementations out there, they will anyway.

chaos
What implementations would those be?
Matthew Crumley
+1  A: 

One difference is that the variables that you declare in the function are local, so they goes away when you exit the function and the don't conflict with other variables in other code.

Guffa
+1  A: 

Is there a parameter and the "Bunch of code" returns a function?

var a = function(x) { return function() { document.write(x); } }(something);

Closure. The value of something gets used by the function assigned to a. something could have some varying value (for loop) and every time a has a new function.

stesch
+1; I prefer an explicit `var x = something;` in the outer function over `x` as parameter, though: imo it's more readable this way...
Christoph
@Christoph: If the value of "something" changes after the function gets created, then it will use the new value and not the one at the time of its creation.
stesch
@stesch: where did you get that from? As far as I know, that's not the case; the only way to get real references in JS is by using the arguments-object, but even that doesn't work in all browsers
Christoph
@Christoph: "JavaScript: The Good Parts", Douglas Crockford (O'Reilly)
stesch
@stesch: it doesn't work the way you describe it: the new value will be used if you drop the variable `x` and depend directly on the lexical scope, ie `document.write(something)`...
Christoph
...`x` is only necessary (either as parameter or local var) if you want to retain the value at creation even when it changes in the outermost scope
Christoph
+1  A: 

Self-invocation (also known as auto-invocation) is when a function executes immediately upon its definition. This is a core pattern and serves as the foundation for many other patterns of JavaScript development.

I am a great fan :) of it because:

* It keeps code to a minimum

* It enforces separation of behavior from presentation

* It provides a closure which prevents naming conflicts

Enormously – (Why you should say its good?)

* It’s about defining and executing a function all at once.

* You could have that self-executing function return a value and pass the function as a param to another function.

* It’s good for encapsulation.

* It’s also good for block scoping.

* Yeah, you can enclose all your .js files in a self-executing function and can prevent global namespace pollution. ;)

More here:

http://wp.me/pgwfF-3v

M A Hossain Tonu