(Function (window, undefined) {}) () in jquery

Source: Internet
Author: User
01 when reading jQuery code today, I found the following statement: 02 (function (window, undefined) {03 & hellip; // codegoeshere04}) (window ); 05window is certainly okay, indicating the window object in the BOM browser object model. But... Synta 01
When reading jQuery code today, I found the following statement:
02
(Function (window, undefined ){
03
... // Code goes here
04
}) (Window );
05
Window must be okay, indicating the window object in the BOM browser object model. But why is there a parameter named undefined? It was hard to understand at first. I went to the Technical Group to ask why.
06
Originally, undefined in Javascript does not appear as a keyword (list of all Javascript keywords. Therefore, users can assign values to them. For example:
07
Varundefined = 'myvalue ';
08
In this way, we assume that jQuery uses the following statement:
09
(Function (window ){
10
... // Code goes here
11
}) (Window );
12
This will inevitably cause the undefined in the intermediate code to be contaminated. By default, the value of an undefined variable is undefined.
13
Varundefined = 'myvalue ';
14
// Or
15
Window. undefined = 'myvalue ';
16
The undefined value in jQuery is changed to the value specified by the user (here is the string 'myvalue '). This will cause jQuery internal exceptions.
17
 
18
JQuery can avoid this problem. When an anonymous function is executed, only one window parameter is passed without undefined. The undefined local variable value in the function body is just undefined.
19
 
20
Compare the following code:
21
 
22
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.