Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: What is the difference between these two statements?

Tags:

jquery

Any difference between these two statements?

$(document).ready(function() {
    // Code
});

$(function() { 
    // Code
});
like image 710
Saar Avatar asked Feb 12 '10 15:02

Saar


People also ask

What is the difference between jQuery and JavaScript?

JQuery is a JavaScript library. It would not have been invented had JavaScript was not there. jQuery is still dependent on JavaScript as it has to be converted to JavaScript for the browser in-built JavaScript engine to interpret and run it.

What is the use of jQuery?

jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous for its philosophy of “Write less, do more”. Please refer to the jQuery Tutorial and jQuery Examples articles for further details.

Would jQuery not have been invented if JavaScript was not there?

It would not have been invented had JavaScript was not there. jQuery is still dependent on JavaScript as it has to be converted to JavaScript for the browser in-built JavaScript engine to interpret and run it.

What are the pros and cons of using pure JavaScript over jQuery?

JQuery is concise and one need not write much as scripting already exists. 6. Pure JavaScript can be faster for DOM selection/manipulation than jQuery as JavaScript is directly processed by the browser and it curtails the overhead which JQuery actually has. JQuery is also fast with modern browsers and modern computers.


2 Answers

In resolution, there is no difference, they are equivalent

From the relevant source code

jQuery = window.jQuery = window.$ = function( selector, context ) {
    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init( selector, context );
}

jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {

    /* .... irrelevant code.... */

    // HANDLE: $(function)
    // Shortcut for document ready
    else if ( jQuery.isFunction( selector ) )
        return jQuery( document ).ready( selector );     
   }
}
like image 194
Russ Cam Avatar answered Sep 18 '22 12:09

Russ Cam


If you mean

$(document).ready(function() {/* code here */});

and

$(function() {/* code here */});

then there is no difference between the two. The latter is just a shortcut that does the same thing as the former.

like image 41
igul222 Avatar answered Sep 19 '22 12:09

igul222