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?
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.
The parentheses wrap around an anonymous function, in order to make it a variable that can be called directly by adding the parameters after it.
(function(param) {
// do stuff
})(param);
The bit at the end is not a namespace, just a parameter. You have probably seen this used for jQuery as:
(function($) {
$('.something').addClass('.other');
})(jQuery);
What this does is pass the jQuery object in to the function, making the $
variable the jQuery object in within the scope of the anonymous function. People like to use the shorthand $
, but it can lead to conflicts with other libraries. This technique removes the possibility of a conflict by passing the fully qualified jQuery
object in and overwriting the $
variable within the scope of that function, so the shortcut can be used.