Search code examples
jqueryunobtrusive-javascriptunobtrusive

Jquery ready function conventions



what is the difference b/w

$(function(){

});

and

(function ($) {
//found this code in jquery uobtrusive ajax
}(JQuery));

first code snippet is simply shorthand for document ready. i have no idea about second code snippet: what does it do and how does it differ from the first code snippet.


Solution

  • The second snippet creates an anonymous function and executes it immediately, without waiting for anything to load.

    It's used to create a local variable (parameter) named $ that refers to jQuery, even if someone calls jQuery.noConflict().

    It also hides internal variables created in the function from the global scope.