A Brief Introduction to jQuery (function () {}) and (function () {}) (jQuery) _ jquery

Source: Internet
Author: User
This article briefly introduces the differences between jQuery (function () {}) and (function () {}) (jQuery, for more information about how to develop jQuery plug-ins, see.

I. First read
JQuery (function (){});
Write All
JQuery (document). ready (function (){});

It means that the ready () method is executed after the DOM is loaded.

2. Check again
(Function () {}) (jQuery );
It is actually executing the () (para) anonymous method, but passing the jQuery object.

(Function ($ ){...}) (JQuery );

Here is actually an anonymous function, as shown below:

Function (arg ){...}
This defines an anonymous function with the parameter arg.

When a function is called, brackets and real parameters are written behind the function. Because of the operator priority, the function itself needs to use parentheses, that is:
(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.

And (function ($ ){...}) (JQuery) is the same. The reason why $ is used only in the form parameter is to avoid conflicts with other libraries, so the real parameter uses jQuery
Equivalent to funtion output (s ){...}; Output (jQuery); or var fn = function (s ){...}; Fn (jQuery );

$ (Function (){...});

Or:

JQuery (function ($ ){
...
});

Allows you to bind a function that is executed after the DOM (excluding images) file is loaded. This function serves as $ (document ). the same as ready (), except that when using this function, you need to wrap all the $ () operators on the page that need to be executed during DOM loading. Technically, this function is connectable-but there are not many cases of actually linking in this way.

Full write is:
$ (Document). ready (function (){
...
});

Iii. Summary

JQuery (function () {}); used to store the code that operates the DOM object. 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 ).
(Function () {}) (jQuery); used to store the code of the Development plug-in. DOM does not necessarily exist when executing the code. Therefore, use the code that directly performs DOM operations with caution.

Related Article

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.