JS has no block-level scope (you can do it yourself or other methods), only the function-level scope and global scope, the function outside the variable function can be found in the use of variables inside the function cannot be accessed outside the variable.
This is because one of the examples in ES6 started. First look at the example
var a=[];For(var i=0; I<10; I++{a[i = function ({console log (I};} A[6] (;
var a=[];For(Let I=0; I<10; I++{a[i = function ({console log (I};} A[6] (; 6
The difference between the two instances is that when I is defined, the loop executes, waiting for the call to execute, I will be assigned to the function body, which is equal to the loop before the array is assigned the value is complete. Wait for the call. Instead of looping, log out the value of I!
Two examples one used the Var one to define the variable with let. Let is a block-level scope, so I for Each loop body is different and independent
In the above code, the variable i
is var
declared by the command and is valid globally, so there is only one variable at the global level i
.
Each cycle, the value of i
the variable will change, and within the loop is assigned to the inside of the a
function of the array, the console.log(i)
inside of the i
point is the global i
. That is, all of the a
members of the array i
, pointing to the same i
, whether the loop to the first several times, resulting in the output of the runtime is the last round of the i
value, that is, 10.
var a=10
function aaa (a) {
alert (a);
}
function bbb () {
var a=20;
AAA ();
}
BBB ();
---result output 10 execution function BBB inside will execute AAA () AAA inside the alert within the a search scope, itself has local variables to output themselves, no words to the scope of AAA to find the upper level of Var a=10 is.
function aaa () {
var a=b=10
}
Alert (a)//error
Alert (b)//10
var a=b=10 this notation in the function, B is actually a global variable, a of course a local variable
After executing AAA (), in the Global Domain alert (a), of course Undefined,alert (b), will pop up 10
var a=10
function aaa () {
Console.log (a)
var a=20
}
AAA ()//undefined variable promotion This function is executed equal to
var a=10
function aaa () {
var A;
Console.log (a)
var a=20
}
Looking at a similar
var a=10
function aaa () {
Console.log (a)
A=20
Console.log (a)
}
AAA ()//10 20
This, it is verified the second, although the nearest principle, but is the nearest to the Var declaration of the variable, this is because the variable without the Var declaration is global, here just modified the value of a. So above is because in the function did not find the Var of a, so went outside to find, a find found, so a on alert out of 10, but the right is a=20, A is really 20, but the alert when the time has not been executed to that ~ ~
var a=10
function aaa () {
BBB ()
Alert (a)
function bbb () {
var a=20
}
}
AAA ()//10
This is because at alert (a), the A in the BBB function is really 20, but it is local to alert (a) at this point, and alert (a) cannot find a in the BBB function at all, so it can't find a in the AAA function, so go outside and look for it. We found the 10.
var a=10
function aaa (a) {
alert (a);
var a=20
}
AAA (a)
The execution process seems to be
var a=10
function aaa (a) {
var A;
alert (a);
var a=20
alert (a);
}
AAA (a)
The passed in 10 assigns the variable a alert (10) after the Var a=20 overrides the previous 10, but the function is not executed here on the alert end. Back alert (a)//20
When a parameter is passed, the base type is passed, and the reference type is referenced.
var a = 5;
var B = A;
b +=3;
alert (a);//5
var a = [n/a];
var b=a;
B.push (4);
alert (a);//[1,2,3,4];
Variable A has no relation to the value of 5 to b b and a.
The array is a reference pass, and the pointer to B also points to the same address, so change a of B also changes
var a = [n/a];
var b=a;
b=[1,2,3,4]
Alert (a)//[1,2,3]
When B is re-assigned, the pointer points to its memory address from the new one, leaving a.
In addition, the parameters are similar to the scope of the variables:
var a=10;
function aaa (a) {
A+=3
}
AAA (a)
Alert (a)//10
Contrast:
var a=10;
function aaa (a) {
a+=3;
Alert (a)//13
}
AAA (a)
Parameters are passed in a value, where a is a local variable, and no matter how it is changed is irrelevant to the outside. Because it is not on the same scope. Similar:
var a=10
function aaa (a) {
Alert (A+10)
}
AAA (30)//40
Alert (a)//10
var a=[1,2,3]
function aaa (a) {
var a=[1,2,3,4]
}
AAA (a)
Alert (a)//"A" in the function is re-assigned, and the outer layer A does not point to the same a
var a=[1,2,3]
function aaa (a) {
A.push (4)
}
AAA (a)
Alert (a)//[1,2,3,4]
After the reference changes the element is added, point to the same a, reference pass pointer, and the value changes.
Variable with (parameter, array) scope transfer problem in JS