Differences between the three methods of declaring global variables in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the similarities and differences between the three methods of declaring global variables in JavaScript. Variable and variable declaration are the most basic concepts of a language and will be quickly mastered by beginners. If you need it, you can refer to it. I hope it will be helpful to everyone. Variables and variable declaration are the most basic concepts of a language and will be quickly mastered by beginners. The same is true for declaring variables in JavaScript. It is simple to use var (keyword) + variable name (identifier ).

Method 1

Var test;
Var test = 5; note that the sentence cannot be included in the function; otherwise, it is a local variable. This is the first way to declare global variables.

Method 2

Test = 5;
If var is not used, assign a value to the identifier test, which implicitly declares the global variable test. Even if the statement is in a function, after the function is executed, test becomes a global variable.

Method 3

Window. test;
Window. test = 5; this method is often used when an anonymous function is executed to make some functions public to the global. For example, the last sentence in JQuery1.5

Window. jQuery = window. $ = jQuery;

If you only use the test variable, there is no difference between the three methods. For example, alert (test) will show 5. However, the three methods are different in some cases. Declare the three variables a1, a2, and a3 respectively in the preceding three methods.

A1 = 11;
Var a2 = 22;
Window. a3 = 33;

1, for in window

For (a in window ){
If (a = 'a1' | a = 'a2 '| a = 'a3 '){
Alert ()
}
}
IE6, 7/8, and 9: Only a3 is displayed. The global variables declared in the first and second ways cannot be obtained when they are declared in the for in window.
Firefox/Chrome/Safari/Opera: a1, a2, and a3 are all popped up. The global variables declared in three ways can be obtained through for in window.


2, delete

Try {
Alert (delete a1 );
} Catch (e) {alert ('cannot delete a1 ')}

Try {
Alert (delete a2 );
} Catch (e) {alert ('cannot delete a2 ')}

Try {
Alert (delete a3 );
} Catch (e) {alert ('cannot delete a3 ')}

The result is as follows:

As you can see,
1. delete a2 all browsers are false. That is, variables declared through var cannot be deleted, and all browsers are consistent. This is also mentioned in the rhino book.

2. Global variables declared through window. a3 cannot be deleted in IE6/7/8, but IE9/Firefox/Chrome/Safari/Opera can.

Although the preceding two differences exist, true is returned when in is used.

Alert ('a1' in window); // true
Alert ('a2 'in window); // true
Alert ('a3 'in window); // true
When you use with to open the window closure of an object, all browsers also behave the same, as shown below:

With (window ){
If (a1 ){
Alert (a1); // 11
}
If (a2 ){
Alert (a2); // 22
}
If (a3 ){
Alert (a3); // 33
}
}

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.