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?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all (2)
曾经蜡笔没有小新

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:

  1. var i

  2. This is the beginning of the for loop

  3. 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); } }
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!