Discussion on defining variables and variable priorities in JavaScript and javascript

Source: Internet
Author: User

Discussion on defining variables and variable priorities in JavaScript and javascript

See the following code:

Copy codeThe Code is as follows:
If (! ("Aa" in window )){
Alert ('Oh my God ');
Var aa = 1;
}
Alert ("aa" in window );
Alert (aa );

Answer the following questions:

Will an error be reported? How many times will it pop up?
Is 2nd alert true or false?
What are the 3rd alert prompts?
Why?
Think about it and test it. If you answer the question correctly, you will not need to read the subsequent articles.

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

Defining variables in JS is too simple. You can directly define a var without having:
Copy codeThe Code is as follows:
Var a = 1;

Here, a is the variable name, and 1 is the variable value. Alas, this is too basic. See the following code:
Copy codeThe Code is as follows:
Var;
Alert ();

When tested with firebug, undefined will pop up. This is a string that everyone is familiar with. It seems that the variable is undefined. But I think I already have var. This is the definition, but there is no value attached.

Let's make a real undefined:

Copy codeThe Code is as follows:
Alert ();

That's right. alert is a variable that never appears at all. What will happen?

Firebug directly reports an error: a is not defined. It indicates that a is not defined. This combination of the previous code is confusing. What is the difference between this definition and the previous definition?

In fact, the previous code is equivalent to the following:

Copy codeThe Code is as follows:
Var a = undefined;
Alert ();

That is to say, when declaring a variable without assigning a value, JS will pass an undefined value to the variable. Note that this is a "value", indicating that a already has a value, this value is called "undefined ".

The following direct alert variable has never appeared, that is, this is the real undefined.

To put it simply: JS does not contain any variable without a value. It is assigned a value when the variable is declared.

Then let's look at the following code:

Copy codeThe Code is as follows:
Alert ();
Var a = 1;

Will this code report an error? The reason is that variable a has not yet appeared in alert.

However, no error is reported, but the undefined value is displayed. It indicates that variable a already exists, but the value is not what we want, but undefined. What's the problem?

Because the declaration of the var variable is the same as that of the function declaration, the above Code is as follows:

Copy codeThe Code is as follows:
Var;
Alert ();
A = 1;

In this way, you will understand.

Therefore, the key to this problem is that the var statement is advanced to the top of the scope, but the value is not-so tangled, I don't know why. I personally think this is a defect of JS.

Now there is a kind of code habit that advocates putting all the variable declarations in front of the scope, probably taking this into account-even if you don't write them in front, JS will advance to the front.

Now let's talk about the answer to the first question:

Only two alert pops up, while alert in if is not executed. Because of the advance of var declaration, the real code looks like this:

Copy codeThe Code is as follows:
Var aa;
If (! ("Aa" in window )){
Alert ('Oh my God ');
Aa = 1;
}
Alert ("aa" in window );
Alert (aa );

Although aa is null, 'A' in window is true because a does exist and the value is undefined. Therefore, if code is not executed. I will not talk about the next two alert events.

I personally think this is a very boring problem. We should understand why, but despise him.

The above question is why I wrote this article. I saw this code from an online article, but there is no answer in it. I can't ride my sister, I ran to stackoverflow and asked for help. The answer is this article.

However, this is a basic problem !!!

Haha, forgive me. There is another problem:
Copy codeThe Code is as follows:
Var B = {}
Alert (B. aa );
Alert (B. aa. bb );

This is also a way to declare variables. Will this code report an error? Why?


A small problem of variable priority in javascript

If the local name is the same as the global name, and the global name has been replaced by local variables, the final output result of the function in vitro should also be, 2, 0. Why is the last one out of 1, 2, 0.
====================
You still need to understand the scope of variables.
If the same name exists, the local priority is given to the global one. This means that the local priority is in a scope (such as a function). If a variable name is the same as a global variable, the current scope (function) the global variable name is ignored, and the local variable value is used. not to replace global variables. (Do you understand that the alternative is to change? This is an incorrect understanding .)
One sentence:
When a function has a variable with the same name as a global variable, the local variable value is used, but the values of global variables in the function in vitro are not affected.

Variable definition in javascript

There are two mistaken people upstairs. It's wrong to say anything without VAR ~

First, you can use var a = document. myform. myinput. value outside the function to obtain the value of the form. You can add VAR or not add VAR.

You didn't paste all programs. I only make the following guesses about your error: Your var a = document. myform. myinput. before the value statement is defined in a form, the program will prompt "empty or not an object" during execution. If you write this statement after the form, it will not be wrong, however, when the code is executed, the user does not input data at all. Such a global variable value is only the default value when your form is created. You need to dynamically obtain the content filled by the user, it is required to trigger the call of the event after the user input in the function.

You can write the following two very short codes for testing. The following code can be correctly executed:
<Form name = myform> <input type = text name = myinput value = 'abc'> </form>
<Script type = text/javascript>
Var a = document. myform. myinput. value;
Alert ();
</Script>

The following code prompts an error:
<Script type = text/javascript>
Var a = document. myform. myinput. value;
Alert ();
</Script>
<Form name = myform> <input type = text name = myinput value = 'abc'> </form>

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.