for(var i=0;i<=5;i++){
setTimeout(function(){
console.log(new Date(),i)
},i*1000)
}
console.log(new Date(),i,'for循环外层')
答案:
Mon Mar 30 2020 21:47:31 GMT+0800 (中国标准时间) 6 "for循环外层"
Mon Mar 30 2020 21:47:31 GMT+0800 (中国标准时间) 6
Mon Mar 30 2020 21:47:32 GMT+0800 (中国标准时间) 6
Mon Mar 30 2020 21:47:33 GMT+0800 (中国标准时间) 6
Mon Mar 30 2020 21:47:34 GMT+0800 (中国标准时间) 6
Mon Mar 30 2020 21:47:35 GMT+0800 (中国标准时间) 6
Mon Mar 30 2020 21:47:36 GMT+0800 (中国标准时间) 6
那么我们把程序换成let会怎么样子呢?
for(let i=0;i<=5;i++){
setTimeout(function(){
console.log(new Date(),i)
},i*1000) }
console.log(new Date(),i,'for循环外层')
Uncaught ReferenceError: i is not defined
at <anonymous>:7:24
(anonymous)
Mon Mar 30 2020 22:18:25 GMT+0800 (中国标准时间) 0
Mon Mar 30 2020 22:18:26 GMT+0800 (中国标准时间) 1
Mon Mar 30 2020 22:18:27 GMT+0800 (中国标准时间) 2
Mon Mar 30 2020 22:18:28 GMT+0800 (中国标准时间) 3
Mon Mar 30 2020 22:18:29 GMT+0800 (中国标准时间) 4
Mon Mar 30 2020 22:18:30 GMT+0800 (中国标准时间) 5
let定义了一个块级变量,在for循环中使用let定义的变量,作用域只在for循环中。那么每一个循环中都有一个专属的变量i,只有i小于等于5时,才会有当前循环,故输出的i的最大值也仅仅是5。又因为i是块级别作用域,故for循环外的console.log()因为暂时性死区报错。
以上,你get到了吗?