Let's use and Const

ES2015 (ES6) adds two important new JavaScript Keywords: the let and const .

Let variables declared valid only within the block where the let command, const declare a constant read-only, once declared value, the constant can not be changed.

1, the let command

let command has the following characteristics:

The inner (1) valid block

ES2015 (ES6) adds two important new JavaScript Keywords: the let and const . let variables declared valid only within the block where the let command, const declare a constant read-only, once declared value, the constant can not be changed.

. 1  {
 2      the let A =. 1 ;
 . 3      var B = 2 ;
 . 4      the console.log (A); // output. 1 
. 5      the console.log (B); // Output 2 
. 6  }
 . 7 the console.log (A); // given a ReferenceError: IS A Not defined 
. 8 the console.log (B); // output 2

(2) can not be repeated statement

let var can only be declared once declared many times:

1 let a = 1;
2 let a = 2;//报错 Identifier 'a' has already been declared
3 var b = 3;
4 var b = 4;
5 console.log(a);
6 console.log(b);//输出4

The for loop counter is suitable for let

 1 for (var i = 0; i < 10; i++) {
 2   setTimeout(function(){
 3     console.log(i);
 4   })
 5 }
 6 // 输出十个 10
 7 for (let j = 0; j < 10; j++) {
 8   setTimeout(function(){
 9     console.log(j);
10   })
11 }
12 // 输出 0123456789

Var variable i is declared valid in the global scope, so that only a global variable i, of each cycle, the timer inside the setTimeout i refers to the global variable i, and in the circulation cycle is ten setTimeout after the end of the implementation, so this time i is 10.

Let the variable j is declared, the current j is valid only in the current round of cycle, each cycle of j are in fact a new variable, so setTimeout timer inside the j actually different variables, and final output 12345. (If the variable j is each cycle of re-statement, how to know the value of the previous cycle? This is because the internal JavaScript engine will remember the value of the previous cycle).

(3) enhance the variable does not exist

let the variable lift does not exist, var variable will enhance:

1 console.log(a);  //ReferenceError: a is not defined
2 let a = "apple";
3 
4 console.log(b);  //undefined
5 var b = "banana";

B exist with variable var to declare variables increase, so when the script started running, b already exists, but has no value, so the output will be undefined. Does not exist with a variable let you declare a variable lift, before declaring a variable, a does not exist, so the error.

(4) temporary dead zone

As long as there is a block-level scope let command, variable declared it would "bind" (binding) in this area, no longer subject to external influences.

1 var tmp = 123;
2 
3 if (true) {
4   tmp = 'abc'; // ReferenceError
5   let tmp;
6 }

The above code, there is a global variable tmp, but let another block-level scope declares a local variable tmp, causing the latter to bind this block-level scope, so before let you declare a variable, tmp assignment of error.

ES6 clearly defined, and if there is let const command block, this block these commands variable declarations, from the outset to form a closed scope. Those who would use these variables in a statement before the error is reported.

In short, in the code blocks before the let command to declare variables, the variables are not available. This is grammatically called "temporary dead zone" (temporal dead zone, referred to TDZ).

 1 if (true) {
 2   // TDZ开始
 3   tmp = 'abc'; // ReferenceError
 4   console.log(tmp); // ReferenceError
 5 
 6   let tmp; // TDZ结束
 7   console.log(tmp); // undefined
 8 
 9   tmp = 123;
10   console.log(tmp); // 123
11 }

The above code, before the let command to declare the variable tmp, tmp variables are all "dead zone."

"Temporary dead zone" also means typeof no longer be a hundred percent safe operation.

1 typeof x; // ReferenceError
2 let x;

 

Further, the following code will be given, and the different behavior var.

1  // not being given 
2  var X = X;
 . 3  
. 4  // given 
. 5 the let X = X;
 . 6  @ a ReferenceError: X IS Not defined

The code above error, but also because the temporary dead zone. When using let you declare a variable, as long as the variable has not been declared completed before use, it will error. Above this line fall into this case, the declaration in the variable x has not been completed before the execution, the value of x will pick up, resulting in an error "x undefined."

ES6 temporary provisions of the dead and let, const statement does not appear to enhance variable, primarily to reduce run-time error that prevents you use the variable before the variable declaration, leading to unexpected behavior. Such errors in ES5 is very common, there is now such a provision, it is easy to avoid such errors.

In short, the essence of the dead zone is temporary, as long as one into the current scope, variables to be used already exist, but are not available, and only until you declare a variable of lines of code that appear before they can obtain and use the variable.

2, const command

const declare a read-only variable, it may not be changed after the statement. Means that, once the declaration must be initialized, otherwise it will error.

Basic usage:

1 const PI = "3.1415926";
2 PI  // 3.1415926
3 
4 const MY_AGE;  // 报错 SyntaxError: Missing initializer in const declaration

Const variable declaration may not change the value, which means, once declared const variables, you must initialize the immediate future can not be left to the assignment.

1 const foo;
2 // 报错 SyntaxError: Missing initializer in const declaration

The above code indicates that, for a const, it is not only the assignment statement, the error will be. Const same scope and let command: block-level role only in the region where it is declared effective.

1 if (true) {
2   const MAX = 5;
3 }
4 
5 MAX // Uncaught ReferenceError: MAX is not defined

Constant const command statement is not the upgrade, there are also temporary dead zone, can only be used in the back position statement.

1 if (true) {
2   console.log(MAX); // ReferenceError
3   const MAX = 5;
4 }

The above code is called before the constant MAX statement, the result of an error. Constant const statement, also let the same statement can not be repeated.

. 1  var Message = "the Hello!" ;
 2 the let Age = 25 ;
 . 3  
. 4  // these two lines are given 
. 5 const Message = "Goodbye!" ;
 . 6 const Age = 30;

Temporary dead zone:

1 var PI = "a";
2 if(true){
3   console.log(PI);  //报错 ReferenceError: PI is not defined
4   const PI = "3.1415926";
5 }

ES6 clear that, if there is the code block or let const, these variables will block command statements from the beginning of the block to form a closed scope. The code blocks before it can declare variables given PI.

Points to note

How do const variable initialization after the declaration is not allowed to change? In fact, in fact, the value of const variables constant is not guaranteed, but to ensure variable points to the memory address of the stored data does not allow changes. At this point, you might have thought, the way simple type and save the value of a composite type is different. Yes, for the simple types (Number value, string string, Boolean Boolean), that value is stored in the memory address pointed to by the variables, declared const so simple type variable equal to a constant. Complex types (objects Object, Array array, function function), a variable memory address pointed actually holds a pointer to the actual data, it can only guarantee a pointer const is fixed, as the data structure pointer to constant change I can not control, so be careful when using a complex type declaration const object.

 

Guess you like

Origin www.cnblogs.com/jpwz/p/12056231.html