慕妹3146593
function makeClosures(arr, fn) { let result = new Array(); // 如果用var,注意闭包问题 for (let i = 0, len = arr.length; i < len; i++) { result[i] = function () { return fn(arr[i]); }; } return result; } let testArr = [1, 2, 3] let fn = function (x) { return x * x; } // 条件一, 返回一个函数数组,长度和arr相同 let result = makeClosures([1, 2, 3], fn); // 条件二, result[i]() 和 fn(arr[i]) 结果相同 console.log('测试 i = 0', 'result[i]: ', result[0](), ' fn(arr[i]): ', fn(testArr[0])) // 1 console.log('测试 i = 1', 'result[i]: ', result[1](), ' fn(arr[i]): ', fn(testArr[1])) // 4 console.log('测试 i = 2', 'result[i]: ', result[2](), ' fn(arr[i]): ', fn(testArr[2])) // 9