Basic jQuery framework: prototype mode structure, return selector instance, access prototype method, and self-executed anonymous function. For details, refer
I. prototype mode structure
The Code is as follows:
// Define a jQuery Constructor
Var jQuery = function (){
};
// Extended jQuery prototype
JQuery. prototype = {
};
The above is a prototype structure, a jQuery constructor and a prototype object of the jQuery instantiated object, which is generally used as follows:
The Code is as follows:
Var jq = new jQuery (); // The variable jq instantiate the jQuery constructor using the new keyword, and then the methods in the prototype object can be used, but jQuery does not.
Ii. Return selector instance
The Code is as follows:
Var jQuery = function (){
// Return selector instance
Return new jQuery. prototype. init ();
};
JQuery. prototype = {
// Selector Constructor
Init: function (){
}
};
Although jQuery does not instantiate an object through the new Keyword, executing the jQuery function still produces an object that instantiates the init selector through the new Keyword, such:
Var navCollections = jQuery ('. nav'); // The navCollections variable saves the DOM object set named nav.
Iii. Access Prototype Method
The Code is as follows:
Var jQuery = function (){
// Return selector instance
Return new jQuery. prototype. init ();
};
JQuery. prototype = {
// Selector Constructor
Init: function (){
},
// Prototype Method
ToArray: function (){
},
Get: function (){
}
};
// Share the prototype
JQuery. prototype. init. prototype = jQuery. prototype;
We generally get used to calling the selector instance object returned by the jQuery function as a jQuery object. We can use it like this:
The Code is as follows:
JQuery. ('. nav'). toArray (); // convert the result set to an array
Why can I use the toArray method? That is, how to allow jQuery objects to access the methods in the jQuery. prototype object? You only need to share the prototype object of the instantiated selector object with the jQuery. prototype object. The code above is as follows:
The Code is as follows:
JQuery. prototype. init. prototype = jQuery. prototype; // share the prototype
Iv. Self-executed anonymous Functions
The Code is as follows:
(Function (window, undefined ){
Var jQuery = function (){
// Return selector instance
Return new jQuery. prototype. init ();
};
JQuery. prototype = {
// Selector Constructor
Init: function (){
},
// Prototype Method
ToArray: function (){
},
Get: function (){
}
};
JQuery. prototype. init. prototype = jQuery. prototype;
// Undo local variables and functions after anonymous functions are executed
Var a, B, c;
Function fn (){
}
// Make jQuery a global variable
Window. jQuery = window. $ = jQuery;
}) (Window );
The local variables and functions declared in the Self-executed anonymous function are revoked after the anonymous function is executed. The memory is released and only the jQuery global variable interface is reserved.
Source: http://www.cnblogs.com/yangjunhua/archive/2012/12/27/2835989.html