为什么 JavaScript const 与 for in 循环配合得很好
Why JavaScript const works well with for in loop
为什么 JavaScript const
和 for in
循环中的 let
一样? const
用于在 EC6 中声明常量。那么为什么 const num
值在 for in
的每次迭代中得到更新?
For in with let
for (let num in nums) {
console.log(num); // works well, as usual
}
对于 in with const
for (const num in nums) {
console.log(num); // why const value getting replaced
}
Why the const num
value getting updated in each iteration of the for in
?
它没有得到更新。 ,它的范围是循环块,并在每次迭代时创建一个新的 const
变量,用相应的 属性 键初始化。
Why JavaScript const works same as let in for in loop?
根据定义,const
的块作用域类似于 let
。
Then why the const num value getting updated in each iteration of the for in?
不是。由于它是块作用域的,每次循环时,旧常量都会退出作用域并创建一个新常量。
也许(不确定)适用于它声明的范围。您似乎在 for 语句的范围内声明常量,因此每次新迭代都会将其删除并重新声明。所以每次都有不同的值。
猜测,不确定...
为什么 JavaScript const
和 for in
循环中的 let
一样? const
用于在 EC6 中声明常量。那么为什么 const num
值在 for in
的每次迭代中得到更新?
For in with let
for (let num in nums) {
console.log(num); // works well, as usual
}
对于 in with const
for (const num in nums) {
console.log(num); // why const value getting replaced
}
Why the
const num
value getting updated in each iteration of thefor in
?
它没有得到更新。 const
变量,用相应的 属性 键初始化。
Why JavaScript const works same as let in for in loop?
根据定义,const
的块作用域类似于 let
。
Then why the const num value getting updated in each iteration of the for in?
不是。由于它是块作用域的,每次循环时,旧常量都会退出作用域并创建一个新常量。
也许(不确定)适用于它声明的范围。您似乎在 for 语句的范围内声明常量,因此每次新迭代都会将其删除并重新声明。所以每次都有不同的值。
猜测,不确定...