for循环简单? 谁能说的通俗点啊? 纠结的不明白
let
arr = [],
arr2 = [],
arr3 = [];
//正常写法
for (var i=0; i<10; i++) {
arr[i] = function () {
console.log(i)
}
}
console.log(arr[5]()) //10
//闭包
for (var i=0; i<10; i++) {
arr2[i] = function (num) {
return function () {
console.log(num)
}
}(i)
}
console.log(arr2[5]()) //5
//es6
for (let i=0; i<10; i++) {
arr3[i] = function () {
console.log(i)
}
}
console.log(arr3[5]()) //5
** 使用了es6的let声明,使用较新的chrome浏览器查看不会报错 **