(Function ($) {...}) (Jquery), jqueryfunction in jQuery

Source: Internet
Author: User

(Function ($) {...}) (Jquery), jqueryfunction in jQuery

What do you feel when you first see "(function ($) {...}) (jQuery? Hehao, I still couldn't help but scold me from the bottom of my heart-fuck, what's the fuck. I feel that I am so ignorant about Jquery. I am so busy today and want to explain how "(function ($ ){...}) (jQuery) "How to understand:

Code 1:

<Html> 

Note: When you run the above Code in a browser, a dialog box will pop up with the content GaoHuanjie.

The JavaScript script executed in the code above and "(function ($ ){...}) (jQuery) "is the same, that is," (function ($ ){...}) (jQuery) "in the function will be automatically executed, So how should we understand" (function ($ ){...}) (jQuery? "(Function ($ ){...}) (jQuery) "actually represents an anonymous function in the called State: function ($ ){...} is the defined anonymous function, the parameter is $ (the reason why the parameter is declared as $ is not in conflict with other libraries ); to call this function, brackets and real parameters (jQuery here) are added to the end of the anonymous function. However, because of the operator priority, the function itself also needs to be enclosed in brackets, therefore, parentheses are added for anonymous functions.
Explain questions:

1. Why does jQuery is not defined:

<Html> Well, the browser does have a problem when parsing the above js script. If you add double quotation marks to jQuery and run the above script again using the browser, there will be no error. Why is there an error if you do not add it, in fact, jQuery is still a variable here. if you introduce the jQuery Library to the above Code and run the above Code in the browser again, there will be no problem:

<Html> 

[Download resources by 0]

After the jQuery library is introduced, the error does not occur because the jQuery variable is defined in the jQuery library.

2. Compared with $ (function () {}), (function ($ ){...}) (jQuery) is the execution time only after the webpage DOM is loaded?

No, for example:

<Html> The preceding example reports an error when running in a browser. The cause is: function (){...}) () The execution time of the function is not executed after the DOM is loaded, but is executed from top to bottom with the page. If you change it to the following code, then run the script in the browser again without any problem:

<Html> 

One sentence: "$ (function () {});" is used to store the code that operates the DOM object. When the code is executed, the DOM object already exists; "(function (){}) (jQuery); "is used to store the code of the Development plug-in. DOM does not necessarily exist when the code is executed.


(Function ($) {}) (jQuery)

1 first (function () {}) () is used to create an anonymous method and immediately execute (function (){}) this is the brackets behind the anonymous method, that is, the method is called immediately ).
In this way, you can create a scope to ensure that internal variables do not conflict with external variables, such as $ jQuery and other variables defined in jquery.

2 (function ($) {}) (jQuery) the main function of this writing method is to ensure that jquery does not conflict with other class libraries or variables. First, ensure that the variable name of jQuery does not conflict with the external name (jquery has two internal $ and jQuery are the same thing. the reason for the name is that $ conflicts with other variable names. 2. jQuery has a very low chance of conflict with other variables) and pass in the anonymous object. The anonymous object is named $ (actually the same as jquery), and then you can freely go to (function ($) {}) (jQuery) write your plug-in without considering whether there is a conflict with external variables.

Jquery (function (a) {}) (jQuery );

This is an anonymous function executed. Its real parameter is a jQuery object.
Equivalent:
Function test (){}
Test (jQuery );
This method is generally used to protect local variables from being contaminated by external variables. When writing your own js library, you may as well write it like this:
(Function (w ){
// You can write your js public library here.
Var my_func = function (){}

W. my_func = my_func; // you need to reference the function to the top-level Object window. Otherwise, the external call will fail.

}) (Window );

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.