You are making a function that is immediately being called, with someWord
as a parameter.
views:
144answers:
7It's a way to define an anonymous function and then immediately executing it -- leaving no trace, as it were. The function's scope is truly local. The ()
brackets at the end execute the function -- the enclosing brackets are to disambiguate what is being executed.
Basically this lets you declare an anonymous function, and then by enclosing it in parentheses and writing (someWord)
you are running the function. You could think of it as declaring an object and then immediately instantiating the object.
It's used to create anonymous function (function without name that can be "nested" inside other function) and pass argument to that function. The someWord is passed as argument, and the function can read it using the keyword "arguments".
Simple example of usage:
function Foo(myval) {
(function(){
// Do something here
alert(arguments[0]);
})(myval); //pass myval as argument to anonymous function
}
...
Foo(10);
You're immediately calling an anonymus function with a specific parameter.
An example:
(function(name){
alert(name);
})('peter')
This alerts "peter".
In the case of jQuery you might pass jQuery
as a parameter and use $
in your function. So you can still use jQuery in noConflict-mode but use the handy $
:
jQuery.noConflict()
(function($){
var obj = $('<div/>', { id: 'someId' });
})(jQuery)