JavaScript defines variables and variable priority issues _javascript tips

Source: Internet
Author: User

Look at the following code:

Copy Code code as follows:

if (! () AA "in Window") {
Alert (' Oh my God ');
var aa = 1;
}
Alert ("AA" in window);
alert (AA);

Answer the following questions:

Do you have an error? How many times does it pop up?
is the 2nd alert true or False?
What does the 3rd alert pop up?
Why?
Think, then test, if you answer correctly, then the post will not be read.

-----------------------------

The definition of a variable in JS is too simple, a direct Var, not even Var can be:

Copy Code code as follows:

var a = 1;

Here A is the variable name, and 1 is the variable value. Alas, this is too basic. Look at the following code:
Copy Code code as follows:

var A;
alert (a);

To Firebug Test, will pop up the undefined, this is everybody very familiar with a string, seemingly indicates that the variable is undefined. But I think, I have been var ah, this is the definition of it, but there is no attached value.

Let's have a real, undefined:

Copy Code code as follows:

alert (a);

Yes, it's direct alert. A variable that doesn't appear at all, what happens?

Firebug a direct error: A is not defined. Meaning a is undefined. This combination of the previous code to look, confusing. What is the difference between this undefined and the previous definition?

In fact, the preceding code is equivalent to this:

Copy Code code as follows:

var a = undefined;
alert (a);

In other words, when declaring a variable without assigning a value, JS will give the variable a undefined value, note that this is a "value", stating that A has a value, this value is called "Undefined."

And then direct alert, the variable never appears, which means it's really undefined.

Simply said: JS does not exist without the value of the variable, variable declaration when the value of the assignment.

Then we look at the following code:

Copy Code code as follows:

alert (a);
var a = 1;

Does this code have an error? Because the variable a has not yet appeared in alert.

But this is not an error, but pop-up undefined value. Indicates that the variable a already exists, but the value is not what we want, but undefined. What's the problem?

Because the var variable declaration is the same as the function declaration, it will be in advance, in fact, the above code is like this:

Copy Code code as follows:

var A;
alert (a);
A = 1;

This way you'll understand.

So, the key to this problem is: the Var statement will advance to the top of the scope, but the value does not ——— a good tangle of settings, do not know why to do so. Personally think this is a flaw in JS.

Now there is a code of habit, advocating the variable declaration is placed in front of the scope, presumably to consider this--anyway, even if you do not write in front, JS will advance to the front.

Now give the answer to the first question:

Only two alert is ejected, and the IF alert is not executed because of the advance of the Var declaration, which causes the real code to look like this:

Copy Code code as follows:

var aa;
if (! () AA "in Window") {
Alert (' Oh my God ');
AA = 1;
}
Alert ("AA" in window);
alert (AA);

Although AA is null, it is true when judged with the ' AA ' in window, because a does exist, and the value is undefined. So if code does not execute. I won't say the two alert.

Personal feeling this is a very irrational question, we should understand his reasons, but despise him for this trap.

The above question is also I write this article reason, this piece of code is I to see from a net text, but inside he has no answer, I hundred tore not to ride elder sister, ran to stackoverflow up to ask to get clear. The answer is this article.

But that's a very basic question, actually.!!!

Haha, forgive me, there is another problem behind:

Copy Code code as follows:

var B = {}
alert (B.AA);
alert (B.AA.BB);

This is also a way of declaring variables, so does this code make an error? Why?

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.