Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You forgot at least one :-)</p> <pre><code>function someFunction() { return someThing; } $(someFunction); </code></pre> <p>Generally there is no difference between: <code>$(document).ready(argFunc)</code> and <code>$(argFunc)</code>. The other variations you listed have all different scopes for different things.</p> <p>For example, in your 2nd block you declare someFunction inside a non-global scope, while your first and my example declare it in the global scope, which has implications for reachability.</p> <p>Also, technically, with both of your variations you produce one extraneous function call. Because in this case, all you call is one function (which you can also write like my example).</p> <p>UPDATE1: To add some additional info and to further the discussion about scopes - JavaScript has very loose requirements for existence of variables. If it doesn't find them in the current scope, it wall just traverse the call chain upwards and look for it - until it finds it or not. That is why you can access the jQuery object ($) from anywhere.</p> <p>There is however this:</p> <pre><code>someFunction($) { // You can use $ here like always return someThing; } $(someFunction); </code></pre> <p>This means, that the handlers (there can be more than one) for the document ready event of jQuery get passed jQuery itself as an argument.</p> <p>If you specify this parameter for your function, you'll use that one, if you reference it. Otherwise, you are using the global one. That reduces the length of the upward look-up - chain. That is completely irrelevant from a performance stand point.</p> <p>But, by specifying this as a parameter, you make it absolutely clear where the jQuery object is coming from. Even that might be irrelevant.</p> <p>I just wanted to show, that these callback-type functions in jQuery often take parameters that are useful. So if you are ever stuck and need access to some object you don't have, it might be worthwhile to check the jQuery docs to see, if there is not a parameter, that does what you want.</p> <p>To conclude this update, I very much like the first comment to the question, which is a much better answer than mine.</p> <p>UPDATE2: On the point of multiple callbacks for document ready (<strong>or any event binder in jQuery for that matter</strong>): You can do this:</p> <pre><code>$(func1); // or $(document).ready(func1); $(func2); // or $(document).ready(func2); </code></pre> <p>Both will get called as soon as jQuery fires its document ready event. That might come in handy depending on the perspective. Some would say, it encourages spreading your logic around. Others might say, it allows for cleaner separation of all the things that need to be done on document ready.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload