Basic jQuery framework introduction _ jquery

Source: Internet
Author: User
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

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.