views:

69

answers:

4

Possible Duplicate:
JavaScript scope and closure

What is this for?

(function(){
     //The code to be executed
})(); 

Also, has this anything to do with closures?

A: 

It's a closure and an anonymous function -- key concepts in Javascript. Javascript doesn't have 'true' private properties or fields--but using a closure you can essentially create the equivalent. They are a very important means of organizing JS.

One critical point in this sample is the (); at the very end of the code--adding these parentheses tells Javascript to execute the code immediately--effectively initializing whatever is contained within.

http://www.jibbering.com/faq/notes/closures/


To help clarify (I'm no JS-expert) -- this construct, even if not a pure closure, is frequently seen in conjunction with closures. For example, the below snippet (copied from my link above) uses this syntax to define an internal, hidden method:

function callLater(paramA, paramB, paramC){
    /* Return a reference to an anonymous inner function created
       with a function expression:-
    */
    return (function(){
        /* This inner function is to be executed with - setTimeout
           - and when it is executed it can read, and act upon, the
           parameters passed to the outer function:-
        */
        paramA[paramB] = paramC;
    });
}

...

/* Call the function that will return a reference to the inner function
   object created in its execution context. Passing the parameters that
   the inner function will use when it is eventually executed as
   arguments to the outer function. The returned reference to the inner
   function object is assigned to a local variable:-
*/
var functRef = callLater(elStyle, "display", "none");
/* Call the setTimeout function, passing the reference to the inner
   function assigned to the - functRef - variable as the first argument:-
*/
hideMenu=setTimeout(functRef, 500);

Although: this example does not execute immediately (it lacks the ();) after the inner-function definition. So, in this case, the enclosed function is evaluated at a later point in time--those () make a big different in a functional language.

STW
A closure, as you stated, is a key concept in JavaScript. But it is essentially to keep or avoid references to variables that belong to the execution context of the function that access them. The thing here is that there are not any variables, and that it is not the point of the question after all. I am not making any variable accessible outside its original context. So.., where are any closures here? I saw fit a downvote ("The answer is not useful"). If you want, I can take it out, but your answer has little in common with the rest.
DanC
"there are not any variables". -- there isn't anything within the example, I presumed the case which you were examining likely did contain members such as variables and other functions, leading to this acting as a closure. It's something I've rarely seen for simple scripting/procedural Javascript
STW
Sorry STW, I didn't mean to be rude, but, did you see the other responses? You can argue all day long, but at this point it seems clear what I've asked (given all the other responses), and your answer just didn't fit. This was in fact an exact duplicate of another question I didn't get to find. Thanks anyway for answering, but your answer may be more adequate for another question, perhaps about closures. Also, your answer seems to have changed a lot since your original one.
DanC
A: 

It's used to run a code OUT of global scope. With this, you are using function scope. In this case, the scope of a anonymous function.

This is usefull when you don't want to create global vars, for example.

See:

var b = 2;
(function(){
    var a=1;
    alert("a:" + a); // alerts 1
    alert("b:" + b); // alerts 2
})();
alert("OUT b:" + b); // alerts 2
alert("OUT a:" + a); // undefined

See in jsfiddle.


EDIT:

The syntax ()() is another way to call a function. See

alert(1); // alerts 1
(alert)(2); // alerts 2

See in jsfiddle.

Topera
+2  A: 

I've seen it often recently. I'm guessing:

No need to name the function. It implies it's not reusable. This gives you a local scope to declare variables using var (otherwise, you would add them to global).

gawi
A: 

Its a way of defining a function and immediately calling it. thats what the last (); does.

Moin Zaman