5

I've been enjoying Lynda.com's Jquery Essential Training, and I've noticed that in the beginning the instructor uses:

 Fig. 1
 $("document").ready(function(){
 fun stuff goes here
 });

However, somewhere along the line he starts using:

 Fig. 2
 $(function(){
 fun stuff goes here
 });

From the way he speaks, it sounds as if they are completely synonymous (some inherent jquery shorthand?) but as far as I can tell, it's never explicitly touched upon.

I'm sure someone could clear this up quickly for me. I found this but I believe that question is slightly different--I understand the concept of calling a function on document ready versus one that is globally available; (those functions also have names.)

The instructor uses phantom functions (I think that was the term for a function without a name,) and when typing out Fig. 2, he says "So this will be on document ready..."

asked Dec 8, 2010 at 6:03
3
  • keep learning and let us if you come across any good stuff Commented Dec 8, 2010 at 6:11
  • 4
    not phantom function. Anonymous function! Commented Dec 8, 2010 at 6:12
  • Hahaha, in the FINAL chapter he touches on my question! Commented Dec 8, 2010 at 7:24

2 Answers 2

8

Yes, they are exactly the same, just aliases.

From the jQuery site:

All three of the following syntaxes are equivalent:

$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)
Deduplicator
46k7 gold badges73 silver badges125 bronze badges
answered Dec 8, 2010 at 6:05
Sign up to request clarification or add additional context in comments.

Comments

1

The default context is the document, so if you pass in some random mumbo jumbo string that does not reference an HTML node, it will be the document.

$('fdsljkfdslj').context is document. And because the default context is the document, this means you don't have to specify it and can just feed a function to jQuery, $(function() { });

And I think you mean $(document) instead, since specifying the string document isn't as popular, because document passes the real document object to jQuery. But again, this will be the same as passing nothing or mumbo jumbo string since we pass document literally.

answered Dec 8, 2010 at 6:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.