Chaotic "variable Declaration"

Source: Internet
Author: User

If you have a problem, you can try to solve it. Otherwise, it is a burden to stay in your mind. Of course, you can forget this. If this has never happened, I believe that many technical staff will be more persistent in this kind of problem. Sometimes, an idea in their mind may even make them crazy about turning all the technical books around them to the ground, there is only one purpose. If you want to know the answer to this question, you must make the question clear and clear.

For the open-source world, I really want to invest and do something that I can do. Unfortunately, I only blame myself for being academic and not refined. I often find something related to myself, there has never been any contribution. I don't know when I had the idea of finding answers from the source code. As mentioned in the previous blog,ProgramYou can use debugging, tracking, simulation, and other methods to find the cause. In fact, you can also obtain relevant answers from the source code. Please believe that the essence of the problem can be found in the source code.

For the <simple introduction to MFC> book, the author simulates the MFC process by himself and prompts us to see the source code of the MFC. I like this book very much, although it took me a long time to finish reading this book, the most fundamental thing I like is that it provides us with solutions to problems. A good book not only gives people a fish, but also gives people a fish.

Now, back to the topic of this blog post, why is it a "Chaotic"? I already have an article?

In object-oriented languages such as C # and Java, the scope of variables is critical. Not only are these object-oriented languages, but all languages are actually the same, right? Today, I will not talk about the type of the variable, nor about the private or public access to the variable. I will talk about the scope. C #. Java variables are generally classified into classes according to the scope of action, that is, they are declared using static, and then member variables and method variables. member variables are accessible to the entire object, method variables are accessible only by this method.

What about JavaScript? If you have read the jquery source code, you can see that all jquery's items are in a self-executed anonymous function. In this anonymous function, a Windows global object is passed, jquery and $ are appended to the windows object, which is the entry for accessing jquery.

But clearly, we can see that the source code has a var jquery at the beginning, while the jquery object appended to Windows is at the end, a var, one does not use VaR, instead, it is directly added after wndows as a global variable. This is actually the scope of JavaScript variables, that is, global variables and local variables, because jquery is not an object-oriented language in my mind, there are no classes, interfaces, inheritance, etc, but it can be simulated. I really don't understand it.

The following is a post I am looking for, which introduces the variable declaration,ArticleFrom: http://www.cnblogs.com/snandy/archive/2011/03/19/1988284.html

Variable 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

?
12 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

?
1 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

?
12 Window. test;Window. test = 5;

This method is often used to expose some functions to the global environment after an anonymous function is executed. For example, the last sentence in jquery1.5

?
1 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.

?
123 A1 = 11;VaR A2 = 22;Window. A3 = 33;

 

1, for in window

?
12345 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

?
1234567891011 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.

?
123 Alert ('A1' In Window );// TrueAlert ('A2' In Window );// TrueAlert ('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:

?
1234567891011 With (Window ){ If (A1 ){ Alert (A1 ); // 11 } If (A2 ){ Alert (A2 ); // 22 } If (A3 ){ Alert (A3 ); // 33 } }

 

Related:

 

 

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.