(function ($) {}) (jquery) = = (function ($) {}) (jquery)
is actually an anonymous function
code to store the development plug-in
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:
- var i=3;
- span class= "keyword" >function init () {
- alert (
- }
- (function ($) {
- var i=2;
- function init () {
- alert (
-
-
- init ();
- }) (jQuery);
- init ();
Execution Result:
Inner layer Init:2
outer Init:3
Here is an example of an experiment
<script src= "Js/jquery-1.7.1.min.js" ></script>
<script type= "Text/javascript" >
(function ($) {
Alert (1);
$.A = function (i)
// {
alert (i.name);
This.shows = function (j)
// {
Alert (j);
Alert (3);
ABC ();
def ();
// }
ABC = function ()
// {
Alert (4);
// }
def = function ()
// {
I.callback (666666);
// }
// }
//})($);
var i = new $.a ({
Name: "Sdy",
Callback:function (NO)
// {
alert (no);
alert (1234);
// }
//});
I.shows (123);
function MM ($) {
Alert (2);
$.A = function (i) {
alert (i.name);
This.shows = function (j) {//this is required to write
Alert (j);
Alert (1);
CC ();
}
AA = function () {
Alert ("AA");
}
bb = function () {
Alert ("BB");
}
CC = function ()
{
I.callbank ("callback function");//callback function CallBack It's okay to write wrong. haha
}
}
};
MM (JQuery);
var abc = new $.A ({
Name: "Sdy", Callbank:function (NO)
{
alert (no);
}
});
Abc.shows (1);
(function ($) {
Alert (1);
Alert ($);
}) (JQuery);
function MM ($) {
Alert (2);
//}
MM (JQuery)
(function (str) {alert (str)}) ("AAA");
Equivalent:
function Outputfun (str) {alert (str);};
Outputfun ("Output");
JS anonymous function