What does this "(function(){});", a function inside brackets, mean in javascript? [duplicate]
Solution 1:
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)
Solution 2:
You are making a function that is immediately being called, with someWord
as a parameter.
Solution 3:
It'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.