ES2015 (ES6) adds two important new JavaScript Keywords: the let and const .
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
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";
(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 }
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 }
"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
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.