admin管理员组

文章数量:1134248

What is the difference between these two.

  1. $(document).ready(function(){ ... });

  2. (function(){ ... })();

Are these both functions called at the same time? I know, document.ready will be triggered when the entire HTML page is rendered by the browser but what about 2nd function (self calling anonymous function). Does it wait for browser to complete rendering the page or it is called whenever it is encountered?

What is the difference between these two.

  1. $(document).ready(function(){ ... });

  2. (function(){ ... })();

Are these both functions called at the same time? I know, document.ready will be triggered when the entire HTML page is rendered by the browser but what about 2nd function (self calling anonymous function). Does it wait for browser to complete rendering the page or it is called whenever it is encountered?

Share Improve this question edited Jan 11, 2019 at 22:24 JSON C11 11.8k7 gold badges81 silver badges66 bronze badges asked Jul 15, 2010 at 19:59 Ashit VoraAshit Vora 2,9222 gold badges28 silver badges41 bronze badges 5
  • 18 For what it's worth, $(function() {}); is equivalent to $(document).ready(function() {}); – Ian Henry Commented Jul 15, 2010 at 20:04
  • 1 The self calling anonymous function will be executed whenever it is encountered. Also, actually rendering the document on screen and creating the object model in memory are unrelated. – Anurag Commented Jul 15, 2010 at 20:07
  • related: Why define anonymous function and pass it jQuery as the argument? on which pattern to use with backbone – Bergi Commented Jul 29, 2014 at 19:51
  • 4 You should really accept answers to your questions when they effectively answer it. You have a very low acceptance rate. – leigero Commented Jul 31, 2017 at 18:09
  • The non-jQuery way to do the first one is: document.addEventListener( 'domContentLoaded', function(){...} ); – blerg Commented Apr 26, 2019 at 16:01
Add a comment  | 

5 Answers 5

Reset to default 117
  • $(document).ready(function(){ ... }); or short $(function(){...});

    This Function is called when the DOM is ready which means, you can start to query elements for instance. .ready() will use different ways on different browsers to make sure that the DOM really IS ready.

  • (function(){ ... })();

    That is nothing else than a function that invokes itself as soon as possible when the browser is interpreting your ecma-/javascript. Therefor, its very unlikely that you can successfully act on DOM elements here.

(function(){...})(); will be executed as soon as it is encountered in the Javascript.

$(document).ready() will be executed once the document is loaded. $(function(){...}); is a shortcut for $(document).ready() and does the exact same thing.

The following code will be executed when the DOM (Document object model) is ready for JavaScript code to execute.

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

The short hand for the above code is:

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

The code shown below is a self-invoking anonymous JavaScript function, and will be executed as soon as browser interprets it:

(function(){
  //write code here
})();   // It is the parenthesis here that call the function.

The jQuery self-invoking function shown below, passes the global jQuery object as an argument to function($). This enables $ to be used locally within the self-invoking function without needing to traverse the global scope for a definition. jQuery is not the only library that makes use of $, so this reduces potential naming conflicts.

(function($){
  //some code
})(jQuery);
  1. $(document).ready(function() { ... }); simply binds that function to the ready event of the document, so, as you said, when the document loads, the event triggers.

  2. (function($) { ... })(jQuery); is actually a construct of Javascript, and all that piece of code does is pass the jQuery object into function($) as a parameter and runs the function, so inside that function, $ always refers to the jQuery object. This can help resolve namespacing conflicts, etc.

So #1 is executed when the document is loaded, while #2 is run immediately, with the jQuery object named $ as shorthand.

document.ready run after DOM is "constructed". Self-invoking functions runs instantly - if inserted into <head>, before DOM is constructed.

本文标签: javascriptjQuery documentready vs self calling anonymous functionStack Overflow