当前位置:网站首页>读了一篇博客,重新理解闭包整理一下
读了一篇博客,重新理解闭包整理一下
2022-04-23 13:59:00 【面试被拒1万次】
写业务的时候很少用到闭包,也没有仔细理解,看到一个博客写闭包写的不错就理解一下,整理一下方便自己理解:
引用:https://blog.csdn.net/albertsh/article/details/82906284
print("\nexample 1:");
function counter()
local count = 0;
return function()
count = count + 1;
return count;
end
endfunc = counter();
-- 这里的实质是 func = function() count = count + 1 return count end
-- 以后每次调用 func() 都是调用这个 函数
-- 这是一个标准的计数器,也是一个标准的闭包,也就是说Lua支持这样的语法,闭包中可以在定义之后一直引用外部的变量,并且在返回函数的整个使用生命周期内都可以引用这个变量,加入外部修改了这个变量,闭包中引用的值也会改变
print(func());
print(func());
print(func());
版权声明
本文为[面试被拒1万次]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_38023160/article/details/82970135
边栏推荐
- 记录一个奇怪的bug:缓存组件跳转之后出现组件复制
- Static interface method calls are not supported at language level '5'
- Small case of web login (including verification code login)
- Special test 05 · double integral [Li Yanfang's whole class]
- freeCodeCamp----arithmetic_ Arranger exercise
- 【报名】TF54:工程师成长地图与卓越研发组织打造
- Reading notes: meta matrix factorization for federated rating predictions
- Choreographer full resolution
- 蓝绿发布、滚动发布、灰度发布,有什么区别?
- L2-024 部落 (25 分)
猜你喜欢
随机推荐
Chapter 15 new technologies of software engineering
【报名】TF54:工程师成长地图与卓越研发组织打造
Special test 05 · double integral [Li Yanfang's whole class]
L2-024 tribe (25 points)
Apache seatunnel 2.1.0 deployment and stepping on the pit
Spark入门基本操作
美联储数字货币最新进展
聯想拯救者Y9000X 2020
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
Android interview theme collection
Express middleware ③ (custom Middleware)
RAC environment error reporting ora-00239: timeout waiting for control file enqueue troubleshooting
MySQL index [data structure + index creation principle]
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
Decentralized Collaborative Learning Framework for Next POI Recommendation
Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
Lenovo Saver y9000x 2020
Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
JS force deduction brush question 102 Sequence traversal of binary tree