1.1 jQuery-overall framework

Source: Internet
Author: User

1.11 self-invoking anonymous function
Open the jQuery source code. First, we can see the code structure:
[Javascript] <span style = "font-size: 14px;"> (function (widow, undefined ){
// Code ....
}) (Window) </span>
This is a self-called anonymous function. So what is it? In the first bracket, create an anonymous function. In the second bracket, the function is executed immediately. → What
So why do we need to create a "self-called anonymous function?
By defining an anonymous function, you can create a "private" namespace. the variables and methods of the namespace do not destroy the global namespace.
This is very important and useful. It is also a required function of a js framework. JQuery should be included in a large number of javaScript programs.
Make sure that the variable created by jQuery cannot conflict with the variable used by the program that imports it.
3. the syntax of an anonymous function is called "function direct quantity". The JavaScript syntax needs to enclose the brackets of an anonymous function. However, in fact, there are two self-called anonymous functions in writing (note the red brackets)
// First
(Function (){
Console.info (this );
Console.info (arguments );
} (Window ));

// Type 2
(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); // window is optimized to

5. Why add undefined to the parameter list?

Make sure that undefined is really undefined within the scope of the self-called anonymous function. 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.12 overall architecture
Next, let's take a look at what functions are implemented in self-called anonymous functions and sort them in the order of code:

(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 );

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.