- Implemented exercises for array high-order methods, memory and execution, switch statements, and final review. - Added starter and answer files for each exercise to facilitate learning. - Created a runner HTML file to execute JavaScript code and display console outputs. - Updated README files to include exercise objectives, tasks, and usage instructions.
练习 9:闭包入门
目标
初步理解闭包为什么能把外层变量“记住”。
你要练什么
- 外层变量
- 内层函数
- 闭包的基本效果
- 多个闭包实例互不影响
任务
请完成一个“计数器工厂”脚本,要求:
- 写一个
createCounter函数 - 在函数内部定义
count - 返回一个内部函数
- 每次调用内部函数时,
count都加 1 - 创建两个不同的计数器
- 观察为什么它们各自记住了自己的
count