tags:

views:

132

answers:

5

Any difference between these two statements?

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

$(function() { 
    // Code
});
A: 

Did you mean:

$(function ()
{
  /* ... */
});

They are equivalent in jQuery. The latter is shorthand for the former.

Vivin Paliath
+1  A: 

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.

igul222
+1  A: 

For all intents and purposes these two statements do the same thing. Internally however the second one will call the first one.

ChaosPandion
+5  A: 

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 );     
   }
}
Russ Cam
+1 - I was too lazy to look this up. :)
ChaosPandion
Just happened to have it open in front of me :)
Russ Cam
Equivalent in effect, and at the present time, but they're not exactly the same (obviously). One explicitly requests that a function be bound as an event handler to the "ready" event, and the other is a less specific request to run something at "ready" time, without explicitly fixing that specifically on the "ready" event.
Pointy
@Pointy - I'm not sure I understand what you're saying. If the first argument passed to the `$()` function is a function, then that function will be bound as a handler for the special `ready` event on the `document`. It resolves identically to `$(document).ready(fn);`, although you're right to point out that this is only the current situation, but given the amount of people's code that would break if they changed it, I can't see the jQuery team being able to change the meaning of `$(function() {});` now, if only to find some better way to execute code when the DOM has loaded.
Russ Cam
The point is that today's implementation of jQuery does that, but by making it a little more abstract it makes possible future improvements to the way initialization functions work. This is not my idea; I recall reading it in some jQuery documentation, or maybe a J. Resig blog post or something.
Pointy
+1  A: 

I think it's important to point out that the official jQuery line is that the second syntax:

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

is preferred. Why? Because it provides for the possibility that Mr. Resig & Co. will come up with a brilliant new way to perform on-load initialization that's way better than using the "ready" event. If they do that, and you use the simpler (second) way of doing things, then your code will immediately benefit from the improvement without any need to change anything. If you use the explicit binding to the "ready" event, you're sticking with that approach until you change it.

Pointy
I'd think it highly likely that if a better way of running code when the DOM has loaded is found, the team would opt to change the code in the ready event and still delegate `$(function() { });` to it.
Russ Cam