Differences between jquery (function () {}) and (function () {}) (jquery)

Source: Internet
Author: User
I would like to share some of my experiences when developing jquery plug-ins.
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.

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.


(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 a parameter to call this anonymous 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 var fn = function ($) {....}; FN (jquery );



Is the usual method for initializing jquery objects ..


In layman's terms, the code you need is executed after the page is loaded.

However, sometimes this will make the page beat,


(Funtion () {}) (); execute the function immediately. It is equivalent to declaring a function and calling it directly after the declaration. If the parameter is 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.