(Function ($) {...}) (Jquery) 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:
Code 1
<Script type = "text/javascript"> (function (name) {alert (name) ;}) ("GaoHuanjie"); </script>
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:
Code 2
<Script type = "text/javascript"> (function ($) {alert ($) ;}) (jQuery); </script>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:
Code 3<Script type = "text/javascript" src = "./jquery-1.6.2.js"> </script>
<Script type = "text/javascript"> (function ($) {alert ($) ;}) (jQuery); </script>
[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:
Code 4
<Script type = "text/javascript"> (function () {alert (document. getElementById ("name"). value) ;}) (); </script>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:
Code 5
<Script type = "text/javascript"> (function () {alert (document. getElementById ("name"). value) ;}) (); </script>
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.