FED59. 使用闭包
描述
实现函数 makeClosures,调用之后满足如下条件:var arr = [1,2,3]; var fn = function (x) { return x * x; } var result = makeClosures(arr,fn); (result[1]() === 4) === (fn(arr[1]) === 4) === true
HTML/CSS/JavaScript 解法, 执行用时: 855ms, 内存消耗: 77792KB, 提交时间: 2021-03-22
{"css":"","js":"function makeClosures(arr, fn) {\n let result = []\n for(let i = 0;i < arr.length; i++) {\n result[i] = fn.bind(this,arr[i])\n }\n return result\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 857ms, 内存消耗: 77772KB, 提交时间: 2021-03-02
{"css":"","js":"function makeClosures(arr, fn) {\n var result = [];\n for(var i=0;i<arr.length;i++) {\n result[i] = function(m) {\n return function() {\n return fn(m);\n } \n }(arr[i]);\n }\n return result;\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 858ms, 内存消耗: 77772KB, 提交时间: 2020-12-23
{"css":"","js":"function makeClosures(arr, fn) {\n let result = [];\n for(let i=0;i<arr.length;i++){\n result[i] = fn.bind(this,arr[i]);\n }\n return result;\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 858ms, 内存消耗: 77828KB, 提交时间: 2021-07-13
{"css":"","js":"function makeClosures(arr, fn) {\n return arr.map(function(item){\n return function(){\n return fn(item);\n \n }\n })\n return arr.map(v=>{return function(){fn(v)}});\n var result = new Array();\n for(let i=0;i<arr.length;i++){\n result[i] = function(){\n return fn(arr[i]); //let声明的变量只在let所在代码块内有效,因此每次循环的i都是一个新的变量 \n };\n }\n return result;\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 859ms, 内存消耗: 77772KB, 提交时间: 2021-03-22
{"css":"","js":"function makeClosures(arr, fn) {\n let result = new Array();\n for(let i=0;i<arr.length;i++){\n result[i]=function(){\n return fn(arr[i]);\n }\n }\n return result;\n}","html":"","libs":[]}