Basic Javascript knowledge in Jquery source code (2), jqueryjavascript

Source: Internet
Author: User

Basic Javascript knowledge in Jquery source code (2), jqueryjavascript

In the previous article, jquery source code is called anonymous function self-execution.

(Function (window, undefined) {// code}) (window );

The function defines two parameters window and undefined. The window object is passed in by the execution function.

 

Parameter window

Because of the relationship between the scope chain of the variable object, we know that even if the window object is not passed in, the function can also access it. Therefore, there are two main Meanings of the input window object:

1. Improve Performance

According to the scope chain order of the variable object, the function first searches for the window variable internally, and the variable initially contained in the function is the arguments object, that is, the parameter variable, at the same time, the amount of code inside the function is relatively large. If it is not found, it will continue to look for the include (external) environment, which is the window object in the global environment.

The window object is passed in directly, which reduces the query time and improves the efficiency.

2. Convenient Compression

(Function (e, undefined) {// code}) (window );

View the JQ source code of the compressed version, and see that all windows in the anonymous function are replaced with e, which reduces the number of characters and further reduces the file size.

If this parameter is not used, the window cannot be changed to another identifier, so that the access is not a window object in the external environment.

 

Parameter undefined

The window object in the global environment has an attribute undefined. Its value is "undefined" and cannot be changed.

However, this is not the case in some browsers, such as ie7 and ie8. You can modify the value.

Therefore, to prevent undefined values from being accidentally modified, an undefined parameter is defined and is not passed during execution.

 

Now, this article is here.

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.