I see examples where JavaScript code including jQuery and jslint use the notation below:
(function(){
// do something
})();
instead of:>
It's about scope for functions, too - everything declared within the code block is scoped to that anonymous function only. Things are normally made public by the framework
(function($) {
var localVarOnly = "local";
$.fn.myCoolFunction = function() { // in the case of jquery, make this publicly available
otherCoolFunction(); //alerts hi
alert(localVarOnly); // alerts local
};
function otherCoolFunction() { // scoped to this anonymous function only
alert('hi');
};
})(jQuery);
otherCoolFunction(); // undefined
alert(localVarOnly); // undefined