As long as you have written some JS code, it's very simple to do it with a var. So what happened to it behind the JS compiler? Then start step by step through code.
x = 1;
alert(x);
var y = function() {
alert(x);
var x = 2;
alert(x);
}
y();
The above code will also output: 1, undefined, 2 if you answer correctly. For me, the first reaction will output: 1,1,2. Why does the second output undefined? Above I have clearly defined a global variable x, why can't it be found?
That's because: when the js compiler executes this y function, it will declare the declared variables in its body to the front. For example: var x=2; The compiler will first make a var x declaration in front of the body. In fact, the above code is equivalent to the following code:
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. However, if the var x = 2; is deleted, it does not make a var declaration internally. It will always look up along the scope, and at this time x is global x.
Let’s take a look at a more interesting example.
var a = 1;
function b() {
a = 10;
return;
}
b();
alert(a);
///////////////////////////////////
var a = 1;
function b() {
a = 10;
return;
function a() {}
} b(); alert(a);
The example is simple. The first example is output 10, and the second will output 1. Why is this? Besides, I have returned the second example. It should be output 10! At that time, the JS compiler was causing trouble behind it.
The difference between the two codes is that the second example has an additional function a(){}; so there is nothing in the function body and no calls are made to it.
In fact, the JS compiler will compile function a() {} into var a=function (){} behind the scenes. At this time, there is also a = 10 inside the function; the outside a is still 1; according to the JS scope. I will first look for the internal a, and if I can't find it, I will look for it one level higher.
The most alert(a) will show 1;
Copy the 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 output: 1, undefined, 2 if you answer correctly. For me, the first reaction will output: 1,1,2. Why does the second output undefined? Above I have clearly defined a global variable x, why can't it be found?
That's because: when the js compiler executes this y function, it will declare the declared variables in its body to the front. For example: var x=2; The compiler will first make a var x declaration in front of the body. In fact, the above code is equivalent to the following code:
Copy the 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. However, if the var x = 2; is deleted, it does not make a var declaration internally. It will always look up along the scope, and at this time x is global x.
Let’s take a look at a more interesting example.
Copy the codeThe code is as follows:
var a = 1;
function b() {
a = 10;
return;
}
b();
alert(a);
///////////////////////////////////
var a = 1;
function b() {
a = 10;
return;
function a() {}
} b(); alert(a);
The example is simple. The first example is output 10, and the second will output 1. Why is this? Besides, I have returned the second example. It should be output 10! At that time, the JS compiler was causing trouble behind it.
The difference between the two codes is that the second example has an additional function a(){}; so there is nothing in the function body and no calls are made to it.
In fact, the JS compiler will compile function a() {} into var a=function (){} behind the scenes. At this time, there is also a = 10 inside the function; the outside a is still 1; according to the JS scope. I will first look for the internal a, and if I can't find it, I will look for it one level higher.
The most alert(a) will show 1;