Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a block of code in parentheses mean in JavaScript/jQuery? [duplicate]

Possible Duplicate:
What does (function($) {})(jQuery); mean?

I've seen a lot of jQuery code with the following sort of syntax, but I don't really understand what it means. It shows up in this answer and this answer on a question about code organization. Both talk about namespacing, so I'm guessing that's what it accomplishes.

var foo = (function () {
    var someVar;

    function someFunc() {
        return true;
    }
})();

Is this for namespacing, and how does it work? Sometimes there is a name (the namespace?) in the final set of parentheses, sometimes not. What is the difference between the two?

like image 593
keithjgrant Avatar asked Jan 07 '11 02:01

keithjgrant


People also ask

What does parentheses mean in JavaScript?

In JavaScript, the functions wrapped with parenthesis are called “Immediately Invoked Function Expressions" or "Self Executing Functions. The purpose of wrapping is to namespace and control the visibility of member functions. It wraps code inside a function scope and decrease clashing with other libraries.

What is a block of code in JavaScript?

A block statement is used to group zero or more statements. The block is delimited by a pair of braces ("curly brackets") and contains a list of zero or more statements and declarations.

What does double parentheses mean in JavaScript?

It means that the first function ( $filter ) returns another function and then that returned function is called immediately.

What is inside the function parentheses?

Parentheses have multiple functions relating to functions and structures. They are used to contain a list of parameters passed to functions and control structures and they are used to group expressions to control the order of execution.


1 Answers

The () that wrap the function turns the anonymous function declaration into a function expression that can then be immediately invoked with the () that follows the expression.

In this case, the outer () really isn't necessary since the var foo = would turn it into an expression. Also, the value of foo will be undefined since the function invocation doesn't return anything.

It can be used for creating a new variable scope, since a function is the only way to accomplish that in javascript. (Javascript doesn't have block scope.)

So the someVar variable is not accessible to the outer scope. There may be times when it is desirable to make it accessible in a controlled manner. To do this, you can pass a function out of that scope which references someVar. Then after the function invocation exits, its execution context will remain intact, and someVar will be available in whatever manner the function you passed out provides.

This is called creating a closure.

Let's say you passed a value into the invocation, and assigned it to someVar. You could then return a function out of the invocation to the foo variable. If that function you return references someVar, then you could use that function to get its value.

var foo = (function ( str ) {
    var someVar = str;
/*
    function someFunc() {
        return true;
    }
*/
    return function() {
        alert( someVar );
    };
})( 'somevalue' );

foo(); // alerts 'somevalue'

As you can see, the function now referenced by foo can still access someVar.

Let's say you changed it so that the function returned to foo can accept an argument, which will update the value of myVar.

var foo = (function ( str ) {
    var someVar = str;
/*
    function someFunc() {
        return true;
    }
*/
    return function( n ) {
        if( n ) {
            someVar = n;
        } else {
            alert( someVar );
        }
    };
})( 'somevalue' );

foo(); // alerts 'somevalue'

foo( 'newvalue' ); // give it a new value

foo(); // alerts 'newvalue'

Now you can see, that the function in foo really does access that variable, as it is able to change its value, and reference the new value that it previously set.

like image 130
user113716 Avatar answered Sep 19 '22 17:09

user113716