js containing variable (parameter array) scope delivery problems

    js no block-level scope (you can own closures or other methods to achieve), only function-level scope and global scope, function variables outside of functions which can be found using the outside variable functions which can not be accessed.

This is because a write ES6 example in the beginning. First look at an example

var a = []; for (var i = 0; i < 10; i++) { a[i] = function () { console.log(i); }; } a[6](); // 10

var a = []; for (let i = 0; i < 10; i++) { a[i] = function () { console.log(i); }; } a[6](); // 6
The difference between these two examples is that when i is defined, when the first loop executed, the execution waiting call, i will be assigned to the function body, and so the cycle is completed give array assignment. Waits for the call. Rather than the cycle time to put the value of i log out! 
Two examples of using a custom variable using let a var. let is block-level scope, so each loop i is different and independent of
the above code, the variable iis the varcommand statement are valid globally, so that only a global variable i.
In each cycle, the variable ivalues will change, but the cycle is assigned to the array ainside the function console.log(i), which the ipoint is global i. In other words, all array amembers inside i, point is the same i, whether it is recycled to the first few times, causing the output of run-time is the last round of ithe value, which is 10.

var a=10
function aaa(a){
alert(a);
}
function bbb(){
var a=20;
aaa();

}
bbb();

// --- output results 10 to perform a function which will be executed aaa bbb () aaa inside of a looking alert in scope, have their own local variables, then the output, if not var aaa scope to find the top of a = 10 It is.

function aaa(){

There are a = b = 10

}

// alert(a) //error 

alert(b)// 10

var a = b = 10 in the wording of this function, b is a global variable in fact, a local variable of course
been performed aaa (), where in the whole local alert (a), of course, is undefined, alert (b), will pop 10

 There are a = 10

function aaa(){

console.log(a)

There are a = 20

}

aaa () // undefined variable is equal to enhance the implementation of this function

There are a = 10

function aaa(){

There;

console.log(a)

There are a = 20

}

In a similar look

 There are a = 10

function aaa(){

console.log(a)

 a=20

console.log(a)

}

aaa() //10  20

This, and verifies the second, although the principle of proximity, but it is to find the nearest var declared variable, this is because there is no var variable declaration is global, but here's a revised value. The above is because it is not found in a function of var, then go to the outside, a find to find, so an alert to a 10; but the right is a = 20, 20 of a indeed, but the alert time has not yet executed go there ~ ~

There are a = 10

function aaa(){

  (BBB)

alert(a)

function bbb(){

 There are a = 20

}

}

aaa()// 10

This is because when the alert (a) is, indeed bbb function in a 20, but it is for this time of alert (a) This sentence is local, alert (a) could not find bbb function of a, so it can not find a function in aaa a, Ever go out looking, a look, they found the 10.

 

var a=10
function aaa(a){

alert(a);
var a=20

}
aaa(a)

Implementation process looks like it should be

var a=10
function aaa(a){
var a;
alert(a);
var a=20
//alert(a);
}
aaa(a)

After 10 assigned to the variable passed in a alert (10) var a = 20 before the cover 10, but the function is not executed to end here the alert. Then followed alert (a) // 20

 

When the parameter passing, traditional values ​​basic types, the type of reference passed by reference.

There are a = 5;
var b = a;
b = 3;
alert (a); // 5

var a = [1,2,3];
var b = a;
b.push (4);
alert (a); // [1,2,3,4];

A variable value passed to 5 and a b b no relationship.

Arrays are passed by reference, also the b pointers point to the same address, so that a change also changes b

There are a = [1,2,3];
var b = a;

b=[1,2,3,4]

alert(a) //[1,2,3]

B after being re-assigned, the pointer will point to your new memory address, from a.

Moreover, the scope of the parameters and variables are similar:

 There are a = 10;

function aaa(a){

 a+=3

}

aaa(a)

alert(a) //10

Compared:

 There are a = 10;

function aaa(a){

 a+=3;

alert(a) //13

}

aaa(a)

Parameters passed in the value, which is a local variable, no matter how changes none of them is outside a. Because not on the same scope. similar:

var a=10
function aaa(a){
alert(a+10)
}
aaa(30) //40
alert(a)//10

There are a = [1,2,3]

function aaa(a){

 There are a = [1,2,3,4]

}

aaa(a)

alert (a) // [1,2,3] is a function which is re-assigned, and the outer layer a does not point to the same a

There are a = [1,2,3]

function aaa(a){

a.push(4)

}

aaa(a)

alert(a)// [1,2,3,4]

After changing the addition reference elements, with a point a, the pointer passed by reference, values ​​are also changed.

Short step a thousand miles, no small streams into a mighty torrent

    js no block-level scope (you can own closures or other methods to achieve), only function-level scope and global scope, function variables outside of functions which can be found using the outside variable functions which can not be accessed.

This is because a write ES6 example in the beginning. First look at an example

var a = []; for (var i = 0; i < 10; i++) { a[i] = function () { console.log(i); }; } a[6](); // 10

var a = []; for (let i = 0; i < 10; i++) { a[i] = function () { console.log(i); }; } a[6](); // 6
The difference between these two examples is that when i is defined, when the first loop executed, the execution waiting call, i will be assigned to the function body, and so the cycle is completed give array assignment. Waits for the call. Rather than the cycle time to put the value of i log out! 
Two examples of using a custom variable using let a var. let is block-level scope, so each loop i is different and independent of
the above code, the variable iis the varcommand statement are valid globally, so that only a global variable i.
In each cycle, the variable ivalues will change, but the cycle is assigned to the array ainside the function console.log(i), which the ipoint is global i. In other words, all array amembers inside i, point is the same i, whether it is recycled to the first few times, causing the output of run-time is the last round of ithe value, which is 10.

var a=10
function aaa(a){
alert(a);
}
function bbb(){
var a=20;
aaa();

}
bbb();

// --- output results 10 to perform a function which will be executed aaa bbb () aaa inside of a looking alert in scope, have their own local variables, then the output, if not var aaa scope to find the top of a = 10 It is.

function aaa(){

There are a = b = 10

}

// alert(a) //error 

alert(b)// 10

var a = b = 10 in the wording of this function, b is a global variable in fact, a local variable of course
been performed aaa (), where in the whole local alert (a), of course, is undefined, alert (b), will pop 10

 There are a = 10

function aaa(){

console.log(a)

There are a = 20

}

aaa () // undefined variable is equal to enhance the implementation of this function

There are a = 10

function aaa(){

There;

console.log(a)

There are a = 20

}

In a similar look

 There are a = 10

function aaa(){

console.log(a)

 a=20

console.log(a)

}

aaa() //10  20

This, and verifies the second, although the principle of proximity, but it is to find the nearest var declared variable, this is because there is no var variable declaration is global, but here's a revised value. The above is because it is not found in a function of var, then go to the outside, a find to find, so an alert to a 10; but the right is a = 20, 20 of a indeed, but the alert time has not yet executed go there ~ ~

There are a = 10

function aaa(){

  (BBB)

alert(a)

function bbb(){

 There are a = 20

}

}

aaa()// 10

This is because when the alert (a) is, indeed bbb function in a 20, but it is for this time of alert (a) This sentence is local, alert (a) could not find bbb function of a, so it can not find a function in aaa a, Ever go out looking, a look, they found the 10.

 

var a=10
function aaa(a){

alert(a);
var a=20

}
aaa(a)

Implementation process looks like it should be

var a=10
function aaa(a){
var a;
alert(a);
var a=20
//alert(a);
}
aaa(a)

After 10 assigned to the variable passed in a alert (10) var a = 20 before the cover 10, but the function is not executed to end here the alert. Then followed alert (a) // 20

 

When the parameter passing, traditional values ​​basic types, the type of reference passed by reference.

There are a = 5;
var b = a;
b = 3;
alert (a); // 5

var a = [1,2,3];
var b = a;
b.push (4);
alert (a); // [1,2,3,4];

A variable value passed to 5 and a b b no relationship.

Arrays are passed by reference, also the b pointers point to the same address, so that a change also changes b

There are a = [1,2,3];
var b = a;

b=[1,2,3,4]

alert(a) //[1,2,3]

B after being re-assigned, the pointer will point to your new memory address, from a.

Moreover, the scope of the parameters and variables are similar:

 There are a = 10;

function aaa(a){

 a+=3

}

aaa(a)

alert(a) //10

Compared:

 There are a = 10;

function aaa(a){

 a+=3;

alert(a) //13

}

aaa(a)

Parameters passed in the value, which is a local variable, no matter how changes none of them is outside a. Because not on the same scope. similar:

var a=10
function aaa(a){
alert(a+10)
}
aaa(30) //40
alert(a)//10

There are a = [1,2,3]

function aaa(a){

 There are a = [1,2,3,4]

}

aaa(a)

alert (a) // [1,2,3] is a function which is re-assigned, and the outer layer a does not point to the same a

There are a = [1,2,3]

function aaa(a){

a.push(4)

}

aaa(a)

alert(a)// [1,2,3,4]

After changing the addition reference elements, with a point a, the pointer passed by reference, values ​​are also changed.

Guess you like

Origin www.cnblogs.com/ZGQ-VIP/p/11856568.html