news 2026/10/21 1:18:28

JavaScript专项算法题(3):闭包

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
JavaScript专项算法题(3):闭包

>>> ORIGIN全文地址:JavaScript专项算法题(3):闭包

推荐:编程鼠标·电竞鼠标

闭包、作用域和运行上下文

挑战1 createFunction

问题:

构建createFunction函数,用于创建和返回函数。当被创建的函数被调用时,它会打印“hello"。

题解:

// CHALLENGE 1functioncreateFunction(){constinnerFunction=()=>{console.log("hello");}returninnerFunction;}/*** Uncomment these to check your work! ***/constfunction1=createFunction();function1();// => should console.log('hello');

挑战2 createFunctionPrinter

问题:

构建接受一个输入值作为参数的createFunctionPrinter函数,用于创建和返回一个特定函数。当特定函数被调用时,其应该打印特定函数被创建时输入createFunctionPrinter中的值。

题解:

// CHALLENGE 2functioncreateFunctionPrinter(input){constinnerValue=input;constinnerFunction=()=>{console.log(innerValue);}returninnerFunction;}/*** Uncomment these to check your work! ***/constprintSample=createFunctionPrinter('sample');printSample();// => should console.log('sample');constprintHello=createFunctionPrinter('hello');printHello();// => should console.log('hello');

挑战3 addByX

问题:

观察下面outer函数的实现代码。注意其会返回一个函数而且那个函数使用了不在其作用域的变量。尝试推断一下运行outer函数得到的输出值,然后构建addByX函数,其会返回一个接受一个输入值作为参数并与x相加的函数。

代码:

// CHALLENGE 3functionouter(){letcounter=0;// this variable is outside incrementCounter's scopefunctionincrementCounter(){counter++;console.log('counter',counter);}returnincrementCounter;}constwillCounter=outer();constjasCounter=outer();// Uncomment each of these lines one by one.// Before your do, guess what will be logged from each function call./*** Uncomment these to check your work! ***/willCounter();willCounter();willCounter();jasCounter();willCounter();functionaddByX(x){letbackpackValue=x;constinnerFunction=(el)=>{returnbackpackValue+el;}returninnerFunction;}/*** Uncomment these to check your work! ***/constaddByTwo=addByX(2);console.log(addByTwo(1));// => should return 3console.log(addByTwo(2));// => should return 4console.log(addByTwo(3));// => should return 5constaddByThree=addByX(3);console.log(addByThree(1));// => should return 4console.log(addByThree(2));// => should return 5constaddByFour=addByX(4);console.log(addByFour(4));// => should return 8console.log(addByFour(5));// => should return 9

挑战4 once

问题:

构建once函数,接受参数为一个回调函数并返回一个特定函数。当特定函数被第一次调用时,其会调用回调函数并返回输出值。如果其不是被第一次调用,则特定函数仅仅返回第一次调用时得到的回调函数返回值,而不是再次运行回调函数。

题解:

// CHALLENGE 4functiononce(func){letcounter=0;letinnerValue=0;constinnerFunction=(el)=>{counter++;if(counter==1){innerValue=func(el);returninnerValue;}else{returninnerValue;}}returninnerFunction;}/*** Uncomment these to check your work! ***/constonceFunc=once(addByTwo);console.log(onceFunc(4));// => should log 6console.log(onceFunc(10));// => should log 6console.log(onceFunc(9001));// => should log 6

挑战5 after

问题:

构建after函数,接受一个数字n和一个回调函数作为参数。回调函数需要在通过after构建的函数运行第n次时才被运行。

题解:

// CHALLENGE 5functionafter(count,func){letcounter=count;constinnerFunction=()=>{counter--;if(counter==0){func();}}returninnerFunction;}/*** Uncomment these to check your work! ***/constcalled=function(){console.log('hello')};constafterCalled=after(3,called);afterCalled();// => nothing is printedafterCalled();// => nothing is printedafterCalled();// => 'hello' is printed

挑战6 delay

问题:

构建delay函数,接受一个回调函数作为第一个参数,一个数值n(单位为毫秒)作为第二个参数。delay函数被调用后,须经n毫秒后才运行回调函数。任何其他赋给特定函数的参数会在n毫秒后被回调函数使用。提示:研究setTimeout();

题解:

// CHALLENGE 6functiondelay(func,wa
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/21 1:18:21

VS项目调试正常,部署到IIS后报错500

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/10/21 1:18:20

FactoryBluePrints|戴森球蓝图库的满产路径

FactoryBluePrints|戴森球蓝图库的满产路径 【免费下载链接】FactoryBluePrints 游戏戴森球计划的**工厂**蓝图仓库 项目地址: https://gitcode.com/GitHub_Trending/fa/FactoryBluePrints 传送带调了三小时还是堵,制造台前的队伍排出去半屏&…

作者头像 李华
网站建设 2026/10/21 1:18:03

D-PHY高速信号完整性调试:从CTS合规到眼图优化实战

1. 项目概述:这不是一次简单的“调通”,而是一场信号边界的极限测绘“MIPI D‑PHY 高速接口信号完整性调试”——光看这个标题,很多刚接触硬件接口的工程师第一反应是:不就是接上线、跑个眼图、调调终端电阻吗?但当你真…

作者头像 李华
网站建设 2026/10/21 1:15:40

3 分钟解锁 Unity 专业版:UniHacker 跨平台破解工具完整指南

3 分钟解锁 Unity 专业版:UniHacker 跨平台破解工具完整指南 【免费下载链接】UniHacker 为Windows、MacOS、Linux和Docker修补所有版本的Unity3D和UnityHub 项目地址: https://gitcode.com/GitHub_Trending/un/UniHacker 打开 Unity 就被许可证弹窗拦住&…

作者头像 李华