Function declaration inside ()

Possible duplicate:
What are the benefits of using (function (window, document, undefined) {...}) (window, document)? Advanced Javascript: why is this function enclosed in parentheses?

I just checked how jquery is written and then on the first line I see this:

(function( window, undefined ) {

});

      

My question is, what is the point or reason for declaring a function inside (

and )

?

+3


source to share


5 answers


In your example, I see no reason for parentheses.

For immediately callable functions, Douglas Crockford recommends and provides sample code as shown below. Source http://javascript.crockford.com/code.html



When a function is to be called immediately, the entire expression call must be wrapped in parens to make it clear that the value that is being created is the result of the function, not the function itself.

var collection = (function () {
    var keys = [], values = [];

    return {
        get: function (key) {
            var at = keys.indexOf(key);
            if (at >= 0) {
                return values[at];
            }
        },
        set: function (key, value) {
            var at = keys.indexOf(key);
            if (at < 0) {
                at = keys.length;
            }
            keys[at] = key;
            values[at] = value;
        },
        remove: function (key) {
            var at = keys.indexOf(key);
            if (at >= 0) {
                keys.splice(at, 1);
                values.splice(at, 1);
            }
        }
    };
}());

      

+3


source


the first line looks like this:

(function( window, undefined ) {

})( window );

      



which is the expression of a function to be called immediately.

+2


source


You typed it wrong, it was probably written:

(function(...) {

    // script

})(...);

      

Used to prevent conflicts. This is a self-starting function. Because of the parentheses at the end, it calls itself.

All variables, objects and functions defined in this function remain in this function.

+1


source


See: http://www.ecma-international.org/ecma-262/5.1/#sec-12.4

It's just part of ECMAScript (and therefore JavaScript) syntax.

You need passwords to call the anonymous function immediately.

function () {}()

raises a syntax error. However, any of these works:

(function () {}())
(function () {})()

      

The main thing is to wrap the entire contents of the function so that external variables do not leak into the function (and vice versa). It's anonymous, so a global function declaration has been added.

+1


source


Since the SLaks are already linked, this is a self-executing function. For the 2 parameters, there is another reason inside, performance and safety.

performance wise, javascript will look for a variable in itself, otherwise it will check its parents and so on. As you can imagine, local variables will always be the fastest.

See undefined for an example: A hacker might just say;

undefined = null;

and now your code doesn't work and will have errors and stuff.

I hope this was helpful.

0


source







All Articles