javascript - Confused about let in for loop in ES6?
大家讲道理2017-05-19 10:14:43
0
2
473
As shown in the picture above, it shows that i has been declared, that is, it cannot be declared repeatedly; while in the picture below But it can be re-declared using let, why?
Why do let statements conflict with var statements? First, the var statement will be promoted to the top of the current function, that is, the order is as follows:
var i
This is the beginning of the for loop
Just started parsing let i = 0 --->Error report Look at the picture again
// for是一个区块,内部又是一个小区块,你的代码可以简写为 { let i = 0; // i作用于这个大区块 { var i = 'abc'; // 此处的i也作用于这个大区块 console.log(i); } } { let i = 0; // i作用于这个大区块 { let i = 'abc'; // i作用于这个小区块 console.log(i); } } // 如果你这么写是可以的 for (var i = 0; i < 3; i++) { let i = 'abc'; console.log(i); } // 简写 { var i = 0; // i作用于这个大区块 { let i = 'abc'; // i作用于这个小区块 console.log(i); } }
Why can let be repeated? Icon
Why do let statements conflict with var statements? First, the var statement will be promoted to the top of the current function, that is, the order is as follows:
var i
This is the beginning of the for loop
Just started parsing let i = 0 --->Error report
Look at the picture again