
您可以在 repo github 上找到这篇文章中的所有代码。
关闭相关的挑战
你好世界
/** * @return {function} */function createhelloworld() { return function (...args) { return "hello world"; };}// usage exampleconst output = createhelloworld();console.log(output()); // => "hello world"
添加
/** * @param {...any} args * @return {function | number} */function add(...args) { let sum = args.reduce((acc, val) => acc + val, 0); function inneradd(...moreargs) { sum += moreargs.reduce((acc, val) => acc + val, 0); return inneradd; } inneradd.getvalue = function () { return sum; }; return inneradd;}// usage exampleconsole.log(add(1).getvalue()); // => 1console.log(add(1)(2).getvalue()); // => 3console.log(add(1)(2)(3).getvalue()); // => 6console.log(add(1)(2, 3).getvalue()); // => 6console.log(add(1, 2)(3).getvalue()); // => 6console.log(add(1, 2, 3).getvalue()); // => 6
和
/** * @param {number} num */function sum(num) { const func = function (num2) { return num2 ? sum(num + num2) : num; }; func.valueof = () => num; return func;}// usage exampleconst sum1 = sum(1);console.log(sum1(2) == 3); // => trueconsole.log(sum1(3) == 4); // => trueconsole.log(sum(1)(2)(3) == 6); // => trueconsole.log(sum(5)(-1)(2) == 6); // => true
柜台
/** * @param {number} initialvalue * @return {function} */function makecounter(initialvalue = 0) { let count = initialvalue - 1; return function (...args) { count += 1; return count; };}// usage exampleconst counter = makecounter(0);console.log(counter()); // => 0console.log(counter()); // => 1console.log(counter()); // => 2//------------------------------// return an object/** * @param {number} initialvalue * @return {{get: function, increment: function, decrement: function, reset: function }} */function makecounter(initialvalue = 0) { let count = initialvalue; return { get: () => count, increment: () => ++count, decrement: () => --count, reset: () => (count = initialvalue), };}// usage exampleconst counterobj = makecounter(0);console.log(counterobj.get()); // => 0counterobj.increment();console.log(counterobj.get()); // => 1counterobj.decrement();counterobj.reset();console.log(counterobj.get()); // => 0
循环
/** * @template t * @param {...t} values * @returns () => t */function cycle(...values) { let index = -1; return function (...args) { index = (index + 1) % values.length; return values[index]; };}// usage exampleconst hellofn = cycle("hello");console.log(hellofn()); // => "hello"console.log(hellofn()); // => "hello"const onofffn = cycle("on", "off");console.log(onofffn()); // => "on"console.log(onofffn()); // => "off"console.log(onofffn()); // => "on"
限制
/** * @param {function} func * @param {number} count * @return {function} */function limit(fn, max) { let count = 0; let value; return function (...args) { if (count < max) { value = fn.call(this, ...args); count++; } return value; };}// usage examplelet i = 1;function incrementby(value) { i += value; return i;}const incrementbyatmostthrice = limit(incrementby, 3);console.log(incrementbyatmostthrice(2)); // i is now 3; the function returns 3.console.log(incrementbyatmostthrice(3)); // i is now 6; the function returns 6.console.log(incrementbyatmostthrice(4)); // i is now 10; the function returns 10.console.log(incrementbyatmostthrice(5)); // i is still 10 as this is the 4th invocation; the function returns 10 as it's the result of the last invocation.i = 4;console.log(incrementbyatmostthrice(2)); // i is still 4 as it is not modified. the function still returns 10.
一次
/** * @param {function} fn * @return {function} */function once(fn) { let ranonce = false; let value; return function (...args) { if (!ranonce) { value = fn.call(this, ...args); ranonce = true; } return value; };}// usage examplefunction func(num) { return num;}const onced = once(func);console.log(onced(1)); // => 1, func called with 1console.log(onced(2)); // => 1, even 2 is passed, previous result is returned
存在或不存在
/** * @param {any} val * @return {true | Error} */function expect(val) { return { toBe: function (arg) { if (val === arg) { return true; } else { throw new Error("Not Equal"); } }, notToBe: function (arg) { if (val !== arg) { return true; } else { throw new Error("Equal"); } }, };}// Usage exampleexpect(5).toBe(5); // Passesexpect(5).notToBe(6); // Passestry { expect(5).toBe(6); // Throws an error} catch (error) { console.log(error.message); // Not Equal}
参考
148。创建一个计数器对象 – bfe.dev2620。计数器 – leetcode2665。计数器 ii – leetcode2665。计数器 ii – leetcode伟大的前端2667。创建 hello world 函数 – leetcode23。创建 sum() – bfe.dev46。实现 _.once() – bfe.dev2704。生存还是毁灭 – bfe.dev161。 tobe() 或 not.tobe() – bfe.dev“你好世界!”程序 – wikipedia.org
以上就是闭包 – JavaScript 挑战的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1495097.html
微信扫一扫
支付宝扫一扫