In jQuery, the difference between jQuery (function () {}) and (function () {}) (jQuery) Is that jqueryfunction

Source: Internet
Author: User

In jQuery, the difference between jQuery (function () {}) and (function () {}) (jQuery) Is that jqueryfunction

Jquery is an excellent Javascr into pt framework. Now we will discuss the functions executed after loading two pages in Jquery.

$ (Document). ready (function () {// write your code here ...});

Code to run when DOM is loaded

Can be abbreviated

jQuery(function(){ });

 

 

(Function ($) {}) (jQuery) What does it mean?

(function(){ })(jQuery);

It is actually executing the () (para) anonymous method, but passing the jQuery object.

Equivalent

function aa($){} aa(jQuery)

 

Is the usual method for initializing jquery objects.

In layman's terms, the code you need is executed after the page DOM is loaded (excluding image download). Because image download is not included, it is more efficient than window. onload.
However, this may sometimes make the page beat. Many JQUERY plug-ins change the style only after loading is complete, and the page will feel beating or flashing. for example, ui. the tab plug-in, where there are multiple page elements, all of which are displayed to form a TAB.

 

(function(){})();

Execute the function immediately. It is equivalent to declaring a function and calling it directly after the declaration;

 

If the parameters are as follows:

(function(str){alert(str)})("output"));

Equivalent:

function OutPutFun(str){alert(str);};OutPutFun("output");

 

 

Summary:

1.

 

jQuery(function(){ });

 

The code used to store DOM objects. When the code is executed, the DOM object already exists. It cannot be used to store the code of the Development plug-in. Because the jQuery object is not passed, the method (function) cannot be called externally through jQuery. method ).

2.

 

(function(){ })(jQuery);

 

It is used to store the code of the Development plug-in. DOM does not necessarily exist when the code is executed. Therefore, use the code that automatically executes the DOM operation with caution.

The development plug-in format is as follows:

(function ($) {        $.fn.test111 = function () {            alert('test')        }    })(jQuery);

Call plug-ins

$("#elementid").test111();


--------------------------------------------------------------------------------


(Function ($) {...}) (jQuery) is actually an anonymous function. If you don't know it, you can continue reading it.
Here is actually the anonymous function (arg ){...} this defines an anonymous function. When the parameter is arg and the function is called, brackets and real parameters are written behind the function. Because of the operator priority, the function itself also needs to be enclosed in parentheses: (function (arg ){...}) (param) This is equivalent to defining an anonymous function with the parameter arg and using param as the parameter to call this anonymous function.
(Function ($) {...}) (jQuery) is the same. The reason why $ is used only in the form parameter is not in conflict with other libraries, so the real parameter uses jQuery.
Equivalent to var fn = function ($) {...}; fn (jQuery );
(Funtion () {}) (); execute the function immediately. It is equivalent to declaring a function and calling it directly after the declaration;
If the parameters are as follows:
(Funtion (str) {alert (str)}) ("output"); equivalent to: funtion OutPutFun (str) {alert (str );}; outPutFun ("output ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.