Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

let在循环中的使用

for (let i = 0; i < 3; i++) {
  // let i = 'abc';
  console.log(i);
}

像上面的循环,会输出0、1、2
let是明确的不允许重复声明的
去掉注释后是下面这样,会输出3个abc我目前理解不明白,有人能够解释一下吗?

for (let i = 0; i < 3; i++) {
  let i = 'abc';
  console.log(i);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
for (let i = 0; i < 3; i++) {
  let i = 'abc';
  console.log(i);
}

等于

let i = 0;
for (; i < 3; i++) {
  let i = 'abc';
  console.log(i);
}

希望你能看出端倪。。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...