jquery Source Code Analysis one: jquery class library overall architecture design analysis

Source: Internet
Author: User
Tags jquery library

If it is to do the web, I believe it is not necessary to make additions and deletions to the Dom, it is believed that we have more or less contact with the JQuery class library, its biggest feature is a powerful selector, so that developers out of the original JS a lot of getElementById, Getelementsbyname ... The official provides an ultra-long method API.

JQuery overall source code, I also in the reading, temporarily recorded. (Why to look at the source code, the reason is very simple----a good understanding of the principle of jQuery two in order to install force).

when using jquery, you first need to introduce a jquery file, and then you can use the $, jquery call directly. Why is it?

Presumably, the students who have written JavaScript know that window is the top object, and like document, location, navigator and so on as window properties, for ease of use, you can omit window., then, JQuery Same principle.

In the source code of $, JQuery is also using this method to introduce window.$=window.jquery=xxx. , the page in the diagram introduces the JQuery library.

// Expose jquery to the global objectwindow.jquery = window.$ = jquery;

two JQuery library used in the process, the general use of two forms, one is $.ajax, $.noconflict, and the other is $ (method parameters) to use

The source shows that JQuery itself is a function, while $.ajax, $.noconflict can be thought of as a static method added by the function object, and the use of $ () is equivalent to calling the $ function method.

jquery = function (selector, context) {//The jquery object is actually just the Init constructor ' enhanced ' return new JQ Uery.fn.init (selector, context, rootjquery);},

  Three jQuery source code in the function of self-executing functions, and self-executing functions of the window, undefined function?

(function(window, undefined) {...}) (window);

self-executing function: The function is automatically executed, and the user cannot be called manually outside, and is executed once.  The self-executing function is equivalent to creating a special scope, which is closed to avoid polluting global variables. Here are just two common ways to do this.

(function () {

function body

})();

+ function A () {

function body

}();

In the JQuery source, the method passes through the window and undefined two parameters.

window: Because when the function is called, it needs to find the variable value, and if the current function scope does not have the variable, it will go up to the previous level to find, directly into the window, can reduce the search time, improve efficiency.

Undefined: After the landlord Test in IE 8, the undefined property value can be modified, at this time the incoming parameter undefined should be maintained undefined initial value.



Not to be continued.

jquery Source Code Analysis one: jquery class library overall architecture design analysis

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.