JS in advance of variable declaration

Source: Internet
Author: User

As long as it is written in a point JS code, very simple a var will be finished. What happens to the JS compiler behind it? That's a step-by-step code.

x = 1; alert (x); var function () {alert (x); var x = 2; alert (x); }y ();      

The above code will also correct you, it will be output separately: 1,undefined,2. For me, the first reaction it will output: 1,1,2. Why does the second output a undefined? In the above I have explicitly defined a global variable x, why can't I find it?

That's because: when the JS compiler executes the Y function, it declares the declaration variable in its body to the front. For example: Var x=2; The compiler first makes a var x declaration at the front of the body. In fact, the above code is equivalent to the following code:

x = 1; alert (x); var function () {  var// at this time X is not yet assigned, so it is undefined.    = 2; alert (x);} Y ();

So it is not difficult to understand x=undefined. But if you put var x = 2; This code is deleted, inside it does not have VAR declaration. It will always look up along the scope, at which point X is Global X.

Now let's look at a more interesting example.

var a = 1; function B () {    = ten;     return ;} b (); alert (a); // ///////////////////////////////// var a = 1; function B () {    = ten;     return ; function A () {}} b (); alert (a);

The example is simple. The first example is output 10, and the second one outputs 1. What is this for? Besides, I return the second example. Supposedly should be output 10! At that time because the JS compiler in the back mischief.

The difference between the two pieces of code is that the second example has a function a () {}; There is nothing in this function body and no calls are made to it.

Actually, the JS compiler will compile function a () {} into var a=function () {} behind. In this case there is also a a=10 for the inside of the function; The outside a is still 1, according to the JS scope. Will first look for internal a, if you can not find the upward level of the search.

The most alert (a) will show 1;

JS in advance of variable declaration

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.