Keyword let in ES6

1. Keyword let

As everyone is familiar with, when we usually write code, we use var to declare a variable. Apart from that, we never come into contact with other keywords. No matter what type of variable we declare, we use var to do it. , very convenient, but ES6 tells you that in addition to var, you can also try let to declare variables.

So, since var is convenient, why should let to declare, so that var declaration must have some deficiencies.

The disadvantages of using var are described below.

1. Look at a piece of code first
var arr = [ ];
for(var i=0;  i<10;  i++){
    arr [i] = function(){
         alert(i)
    }
}
arr [1](); //结果:10
Obviously, the desired result of executing this code is to pop the value corresponding to the index of the arr array. But it is found that no matter the index is how many pop-up results are 10.
Why ?
Continue to execute this code to see:
var arr = [ ];
for(let i=0;  i<10;  i++){
    arr[i] = function(){
         alert(i)
    }
}
arr[1](); //结果:1

The value of the corresponding index popped up! Comparing the two pieces of code, the only difference is that the variable i is initialized using let instead of var during the loop. Why ? ?
It turns out that the variables declared by let only work in the block-level scope they use, and they won't work outside the block-level scope.
Note: Block-level scope, that is, any pair of statements in curly braces belongs to a fast. All variables defined with let in curly braces are invisible outside curly braces. We call it block-level scope.
So back to the code, the for loop contains { }, that is, it contains a block-level scope, and each variable i only works in its own scope. For example, the value of i in the 10th loop will not affect to the 9th cycle.

ok!

2. var declaration of variables will appear "variable promotion".
What ?
Above code:
var a = 1;
(function(){
   alert(a);
   var a = 2;
})();//结果:undefined
At a glance, shouldn't the result be 1, why is it undefined?
The reason is that we also declare and define a variable a in the code block (in the function), which causes the variable to be promoted. The actual code execution order is like this. After reading it carefully, you will understand what is the variable promotion.
var a = 1;
(function(){
   var a;
   alert(a);
   a = 2;
})();
Compare the two pieces of code: var a = 2; This code is split into two parts: the declaration var a ; and the definition a = 2; and the declaration part is lifted, and it is moved to the front of the code block when it runs. As mentioned earlier, this is "variable promotion", and the result is: first execute the declaration, then execute alert(a); the variable a is just declared and has not yet been defined, and undefined is popped up.
So, using the let keyword in a code block won't be hoisted? sure and sure.
var a = 1;
(function(){
   alert(a);
   let a = 2;
})();   // 结果:报错a未定义
Then why the error is reported again, because the variable declared with let is closed in its block-level scope and will not be affected by the external global variable a, and it must be declared before use, so the value of a is not 1. (Because it is not affected by the outside world), it is not undefined (because it is declared first and then used), and it is not 2. If it is used without declaring the definition, it will only report an error.
Note: Remember to declare the definition of the let keyword before using it.
3. Other notes on using let
(1) Within the same block-level scope, it is not allowed to declare the same variable repeatedly.
Error example:
{
  var a =1;
  let a =2;   //报错,因为a已经用var声明过
}
{
  let a =1;
  let a= 2;  // 还是报错,a已经用let声明过。
}
(2) The parameters of the function cannot be re-declared with let inside the function
function say(name){
    let name = 'zhang';  //报错:用let重新声明name参数
   alert(name)
}
say('wang');
The name parameter is redeclared with let in the say() function, No!

summary:

Declaring variables with let only works in block-level scope, suitable for use in for loops, and there will be no variable promotion phenomenon. Within the same code block, the same variable that cannot be repeatedly declared, and the parameters within the function cannot be repeatedly declared.

Subsequent updates...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325119092&siteId=291194637