Analysis of jQuery initialization and Object Construction _ jquery

Source: Internet
Author: User
My previous work and study mainly focused on native js, and I have never been very familiar with jQuery. But jQuery, as one of the best js class libraries today, must take a good time to learn, today, it was just a pain point. I read some code in it and summarized it:

1. The entire class library is defined in an anonymous function to prevent the generation of global variables;
2. Pass undefined as a missing parameter to prevent contamination of the undefined variable;
3. we can see that $ (...) actually, jQuery is returned. fn. the instance of the init object, and then point the prototype of the object to jQuery. prototype (statement jQuery. fn. init. prototype = jQuery. therefore, the generated instances share jQuery. methods and attributes in prototype and implement chained programming;
4. Finally, use window. jQuery = window. $ = jQuery to export jQuery and $ as global variables.

The Code is as follows:


(Function (window, undefined ){
// Define a local copy of jQuery
Var jQuery = (function (){
Var jQuery = function (selector, context ){
// The jQuery object is actually just the init constructor 'enabled'
Return new jQuery. fn. init (selector, context/*, rootjQuery */);
};
//...
JQuery. fn = jQuery. prototype = {
Constructor: jQuery,
Init: function (selector, context, rootjQuery ){
//...
}
//...
};
// Give the init function the jQuery prototype for later instantiation
JQuery. fn. init. prototype = jQuery. fn;
//...
// Expose jQuery to the global object
Return jQuery;
})();
//...
Window. jQuery = window. $ = jQuery;
}) (Window );

Related Article

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.