Why do many javascript libraries look like this:
(function () {
/* code goes here */
})();
It appears to define an unnamed function whic
This is standard way to do namespacing in JavaScript. If you just declare
var my_cool_variable = 5;
it will be global and might conflict with other libraries, that use the same variable.
However, if you do
(function() {
var my_cool_variable = 5;
})();
it is now local variable for anonymous function and is not visible outside of the scope of that function. You still can expose accessible API by not stating var
in front of variable, that way it will be global but now you have a choice.