How JavaScript var declares Variables

Source: Internet
Author: User

As long as you have written some JS Code, a simple var will be done. What happened to the JS compiler? Let's start with the code step by step.
Copy codeThe Code is as follows:
X = 1;
Alert (x );
Var y = function (){
Alert (x );
Var x = 2;
Alert (x );
}
Y ();

The above code will also be correct, and it will output: 1, undefined, 2 respectively. For me, the first response will output: 1, 1, 2. Why does the second output undefined? I have clearly defined a global variable x above. Why can't I find it?

That's because when the js compiler executes this y function, it will advance the declared variables in its body to the beginning for declaration. For example, var x = 2; the compiler first declares var x at the beginning of the body. In fact, the above Code is equivalent to the following code:
Copy codeThe Code is as follows:
X = 1;
Alert (x );
Var y = function () {<BR> var x; // at this time, x has not been assigned a value, so it is undefined.
Alert (x );
X = 2;
Alert (x );
}
Y ();

So it is not difficult to understand x = undefined. But if var x = 2 is deleted, no var declaration is made internally. It will always look up along the scope. At this time, x is global x.
Next, let's look at a more interesting example.
Copy codeThe Code is as follows:
Var a = 1;
Function B (){
A = 10;
Return;
}
B ();
Alert ();
///////////////////////////////////
Var a = 1;
Function B (){
A = 10;
Return;
Function (){}
} B (); alert ();

The example is simple. The first example is output 10, and the second output 1. Why? In the second example, I have returned. We should generate 10 for all the reasons! At that time, the JS compiler was behind the scenes.
The difference between the two pieces of code is that the second example has a function a () {}; so there is nothing in the function body and no call is made to it.

In fact, the JS compiler will compile function a () {} into var a = function (){}. At this time, there is also a = 10 inside the function; some outside is also 1; according to the JS scope. The internal a will be searched first. If it cannot be found, it will be searched again at the upper level.
The first alert (a) will display 1;

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.