Different ways to express a document in jQuery?

Are they the same, i.e. ways to confirm the document:

$(function() { // }); 

and

 $(function($) { // })(jQuery); 

or is there a difference between them, if so, when should I use which?

+6
source share
3 answers

The first is a shortcut for .ready() .

The second is simply not true, since you are trying to call an un-called object.

You probably meant the following:

 // v--------no $ at the beginning (function( $ ) { // simply a new lexical environment with a // local $ parameter pointing to jQuery })(jQuery); 

... although it has nothing to do with the DOM.

In your first example, there is an option that combines the two:

 jQuery(function( $ ) { // DOM ready, and creates a local $ parameter pointing to jQuery }); 
+9
source

Both are not the same.

The first code block is used to perform the function on the finished document, where when the second code block is used, when we want to immediately execute the code block, without waiting for the rest of the code to load. But there is some error in the second part of the code. It should be as shown below.

 (function($) { // })(jQuery); 
+1
source

This is not true:

 $(function($) { // })(jQuery); 

You pass the function $(...) and then call the result. However, the result of $(...) is a jQuery object, which is not a function. You could see it better:

 $( function($) { // } ) (jQuery); 

Typically, there are three versions of document.ready that are all equal to each other:

 $(function() {...}); $(document).ready(function() {...}); $().ready(function() {...}); 
0
source

Source: https://habr.com/ru/post/894457/


All Articles