In JQuery, $ (function () {}) and (function ($) {}) (jquery), $ (document). The difference between ready (function () {}) and so on is explained in detail----reproduced

Source: Internet
Author: User

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

1), principle:

This is actually an anonymous function, as follows:

Function (ARG) {...}
This defines an anonymous function with a parameter of ARG

When calling a function, the parentheses and arguments are written after the function, and because of the precedence of the operator, the function itself needs parentheses, namely:
(function (ARG) {...}) (param)
This is equivalent to defining an anonymous function with a parameter of ARG, and calling this anonymous function as a parameter param

and (function ($) {...}) (jquery) is the same, the reason why only use $ in formal parameters is to not conflict with other libraries, so the argument with jquery
Equivalent to funtion output (s) {...};output (jQuery), or Var fn=function (s) {...}; FN (jQuery);

2), function ( very useful ):

The greatest benefit of this notation is the formation of closures. In (function ($) {...}) (jQuery) functions and variables defined internally can only be valid within this scope.

The concept of whether a function function or a private variable is formed. Like what:

[JavaScript]View PlainCopy
    1. var i=3;
    2. function init () {
    3. Alert ("outer init:" +i);
    4. }
    5. (function ($) {
    6. var i=2;
    7. function init () {
    8. Alert ("inner init:" +i);
    9. }
    10. Init ();
    11. }) (JQuery);
    12. Init ();

Execution Result:

Inner Layer Init:2

Outer Init:3


2, $ (function () {...});  JQuery (function ($) {...}); $ (document). Ready (function () {...})

These three roles are the same.

In JQuery, $ (function () {}) and (function ($) {}) (jquery), $ (document). The difference between ready (function () {}) and so on is explained in detail----reproduced

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.