JQuery source code analysis-01 overall architecture

Source: Internet
Author: User

1. Overall Architecture

1.1 Self-called anonymous Functions Self-invoking anonymous function

Open the jQuery source code, first you will see the code structure:

 

(Function (window, undefined ){

// Jquery code

}) (Window );

1. This isSelf-called anonymous Functions. What is it? In the first bracket, create an anonymous function. In the second bracket, execute immediately.

2. Why is this "self-called anonymous function" created?

By defining an anonymous function, a "private" namespace is created. The variables and methods of the namespace do not destroy the global namespace. This is very useful and a function that must be supported by the JS framework. jQuery is applied to thousands of JavaScript programs, make sure that the variable created by jQuery cannot conflict with the variable used by the program to be imported.

3. the syntax of an anonymous function is called the function directly. The JavaScript syntax needs to enclose the brackets of an anonymous function. In fact, there are two ways to write a self-called anonymous function (note the red right Parenthesis ):

 

(Function (){

Console.info (this );

Console.info (arguments );

} (Window ));

(Function (){

Console.info (this );

Console.info (arguments );

}) (Window );

4. Why should I input a window?

Pass in the window variable so that the window is changed from a global variable to a local variable. When you access the window in the jQuery code block, you do not need to roll back the scope chain to the top-level scope, so that you can access the window more quickly; this is not the key, more importantly, the window as a parameter passed in, you can optimize the code when compression, look at the jquery-1.6.1.min.js:

 

(Function(A, B) {}) (window); // The window is optimized to

5. Why add undefined to the parameter list?

InSelf-called anonymous FunctionsWithin the scope of, make sure that undefined is really undefined. Because undefined can be rewritten, a new value is assigned.

 

Undefined = "now it's defined ";

Alert (undefined );

Browser Test Result:

 

Browser

Test Results

Conclusion

Ie

Now it's defined

Changeable

Firefox

Undefined

Cannot be changed

Chrome

Now it's defined

Changeable

Opera

Now it's defined

Changeable

6. Have you noticed the last semicolon in the source code?

Semicolons are optional, but omitting semicolons is not a good programming habit. For better compatibility and robustness, add semicolons after each line of code to form a habit.

1.2 Overall Architecture

Next let's take a lookSelf-called anonymous FunctionsWhat functions are implemented in the code order:

 

(Function (window, undefined ){

// Construct a jQuery object

Var jQuery = function (selector, context ){

Return new jQuery. fn. init (selector, context, rootjQuery );

}

// Tool function Utilities

// Asynchronous queue Deferred

// Browser test Support

// Data cache

// Queue

// Attribute operation Attribute

// Event processing

// Selector Sizzle

// DOM Traversal

// DOM operation

// CSS operation

// Asynchronous request Ajax

// Animated FX

//Coordinates and size

Window. jQuery = window. $ = jQuery;

}) (Window );

From the above comments, jQuery's source code structure is quite clear and organized, not as obscure and tangled as the code.

The subsequent chapters are displayed in this order.

1.3 Next Preview

If you have read jQuery source code, you will soon find that regular expressions are everywhere, and many JavaScript developers neglect the basic knowledge of regular expressions. to clear this obstacle, in the next chapter, we will first review the basic knowledge of JavaScript regular expressions, and then analyze the regular expressions in jQuery in detail.

Before the official start of source code analysis, do you have any basic knowledge to prepare?

Of course. For example, if you are not familiar with classes and objects in JavaScript APIs, you must have at least one reference manual.

In addition to regular expressions, other knowledge points will be explained in the analysis process without creating new chapters.

Author: "integration of knowledge and Industry"
 

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.