4

Possible Duplicate:
What advantages does using (function(window, document, undefined) { … })(window, document) confer?
Advanced Javascript: Why is this function wrapped in parentheses?

I just checked how jquery is written, then in the first line of it i see this:

(function( window, undefined ) {

});

My question is what is the meaning or reason the declaration of function is inside the ( and )?

Community
  • 1
  • 1
GusDeCooL
  • 5,639
  • 17
  • 68
  • 102

5 Answers5

4

In your example, I see no reason for the parentheses.

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

When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and 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);
            }
        }
    };
}());
Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
2

actually, the first line is like this:

(function( window, undefined ) {

})( window );

which is an immediately-invoked function expression.

ic3b3rg
  • 14,629
  • 4
  • 30
  • 53
1

You typed it wrong, it was probably written:

(function(...) {

    // script

})(...);

It is used to avoid conflicts. This is a self-invoking function. Because of the parentheses at the end, it invokes itself.

All variables, objects and functions defined within this function, stays in this function.

Tim S.
  • 13,597
  • 7
  • 46
  • 72
1

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

This is just part of the ECMAScript (and thus JavaScript) syntax.

You need the parens to call the anonymous function immediately.

function () {}() causes a syntax error. Either of these work, though:

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

The main point is to wrap all of the function's contents so that external variables do not leak into the function (and vice versa). It is anonymous so that there is no global function declaration added either.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

As SLaks already linked, it's a self-executing-function. For the 2 parameters inside is another reason, performance and security.

performance wise, javascript will look into itself for the variable, else he will check his parents and so on. As u can imagine, local variables will be always the quickest.

For the undefined section, here is an example: A hacker can just say;

undefined = null;

and now your code is failing and will have bugs and stuff.

I hope this was helpfull.

Ilinea
  • 62
  • 6