当前位置:网站首页>读了一篇博客,重新理解闭包整理一下
读了一篇博客,重新理解闭包整理一下
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
边栏推荐
- Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
- Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
- L2-024 部落 (25 分)
- Jiannanchun understood the word game
- 初探 Lambda Powertools TypeScript
- Reading notes: meta matrix factorization for federated rating predictions
- Jenkins construction and use
- Reading notes: Secure federated matrix factorization
- Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
- Analysis and understanding of atomicintegerarray source code
猜你喜欢
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
Express②(路由)
Basic SQL query and learning
Building MySQL environment under Ubuntu & getting to know SQL
Using Baidu Intelligent Cloud face detection interface to achieve photo quality detection
Wechat applet
SQL learning | complex query
Decentralized Collaborative Learning Framework for Next POI Recommendation
Dolphin scheduler source package Src tar. GZ decompression problem
MySQL [SQL performance analysis + SQL tuning]
随机推荐
Introduction to spark basic operation
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table
Wechat applet
UNIX final exam summary -- for direct Department
Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
JS force deduction brush question 102 Sequence traversal of binary tree
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
Failure to connect due to improper parameter setting of Rac environment database node. Troubleshooting
Force deduction brush question 101 Symmetric binary tree
初探 Lambda Powertools TypeScript
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Small case of web login (including verification code login)
Dolphin scheduler configuring dataX pit records
Function executes only the once function for the first time
联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
About me
[code analysis (7)] communication efficient learning of deep networks from decentralized data
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
Handling of high usage of Oracle undo
L2-024 tribe (25 points)