【发布时间】:2020-08-04 13:03:35
【问题描述】:
我对在 for 循环中声明的函数的关闭行为感到非常困惑,尤其是在初始化程序中定义的变量:
function createFunctions(){
const functions = []
for(let i = 0; i < 5; i++)
functions.push(() => i);
return functions;
}
const results = createFunctions().map(m => m())
// results: [0, 1, 2, 3, 4]
对
function createFunctions(){
const functions = []
let i;
for(i = 0; i < 5; i++)
functions.push(() => i);
return functions;
}
const results = createFunctions().map(m => m())
// results: [5, 5, 5, 5, 5]
由于在 for 循环中声明的匿名箭头函数捕获了它的作用域,我希望这两种情况都会产生 [5, 5, 5, 5, 5],因为在调用时,i 的值为 5。然而,第一个结果似乎表明在每次迭代中通过循环,我是一个不同的变量。但是,如果您重复测试但初始化的变量是对象而不是数字:
function createFunctions(){
const functions = []
for(let obj = {}, i = 0; i < 5; i++)
functions.push(() => obj);
return functions;
}
const results = createFunctions().map(m => m())
// results: [{}, {}, {}, {}, {}]; results[0] === results[1]: true
我们可以看到返回数组中的所有元素在引用上都是相等的,因此不是不同的变量。因此,似乎函数关闭 for 循环初始化程序中声明的变量的方式会根据变量是否为原始变量而改变,这对我来说听起来很荒谬。
我错过了什么?
【问题讨论】:
标签: javascript functional-programming closures