Many languages have block-level scopes, but JS does not, it uses VAR to declare variables, the function to divide the scope, curly braces "{}" does not limit the scope of var. Variables declared with VAR have the effect of variable promotion (declaration hoisting).
A let is added to the ES6, which can be declared in {}, if, for. The usage is the same as Var, but scoped at the block level, the let declared variable does not have a variable promotion.
Example 1: block-level scope if
|
function
getVal(boo) {
if
(boo) {
var
val =
‘red‘
// ...
return
val
}
else
{
// 这里可以访问 val
return
null
}
// 这里也可以访问 val }
|
The variable Val is declared in the If block, but can be accessed to Val outside of the else block and if.
Change Var to let, that's it.
|
function
getVal(boo) {
if
(boo) {
let
val =
‘red‘
// ...
return val
}
else
{
// 这里访问不到 val
return
null
}
// 这里也访问不到 val }
|
Example 2: block-level scope for
123456 |
function func(arr) { for ( var i = 0; i < arr.length; i++) { // i ... } // 这里也可以访问到i } |
The variable i is declared in the for block, but can also be accessed for outside.
I cannot be accessed by replacing VAR with let,for.
123456 |
function func(arr) { for ( let i = 0; i < arr.length; i++) { // i ... } // 这里访问不到i } |
Example 3: Variable promotion (declared after use)
12345 |
function func() { // val先使用后声明,不报错 alert(val) // undefined var val; } |
The variable Val is declared after the first use, output undefined, and no error.
Change Var to let, it's an error.
12345 |
function func() { // val先使用后声明,报语法错 alert(val) let val; } |
Example 4: variable Promotion (first judgment and later declaration)
123456 |
function func () { &NBSP;&NBSP;&NBSP;&NBSP; if ( typeof val == ' undefined ' ) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //... &NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; var val = " } |
You can also use TypeOf to determine the front of the Var statement
But change the Var to let,if and report grammatical errors.
123456 |
function func () { &NBSP;&NBSP;&NBSP;&NBSP; if ( typeof val == ' undefined ' " { //... &NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; let val = " } |
ES6 rules that if let is present in a code block, the chunk forms a closed scope from the beginning. Any use before the declaration, will be an error. That is, within a code block, using a variable before a let declaration is not available. There is a grammatical term called "temporary Dead Zone" (temporal dead Zone), referred to as TDZ. Of course Tdz does not appear in the ES specification, it is only used to describe the image.
Let's considerations
1. Cannot repeat the declaration
1234567 |
// var和let重复声明 var name = ‘Jack‘ ; let name = ‘John‘ ; // 两个let重复声明 let age = 24; let age = 30; |
Execution times syntax error
2. With let, the anonymous function can be removed from execution.
12345678910111213 |
// 匿名函数写法
(
function
() {
var
jQuery =
function
() {};
// ...
window.$ = jQuery
})();
// 块级作用域写法
{
let
jQuery =
function
() {};
// ...
window.$ = jQuery;
}
|
ES6 block-level scope and new variable declaration (let)