开发者

Why/Where/When should I do this? (enclose js code inside a function and then execute it) [duplicate]

开发者 https://www.devze.com 2023-01-13 17:47 出处:网络
This question already has answers here: 开发者_JS百科 Closed 12 years ago. Possible Duplicate: JavaScript scope and closure
This question already has answers here: 开发者_JS百科 Closed 12 years ago.

Possible Duplicate:

JavaScript scope and closure

What is this for?

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

Also, has this anything to do with closures?


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).


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消