当前位置:网站首页>The beauty of do end usage
The beauty of do end usage
2022-07-29 07:31:00 【The senming Gang is bigger than the black tiger gang】
Why use do end, What problems can it solve ; First , Statement block is not used Curly braces { } , It is do Sentence block end Indicates the beginning and end of a statement block . and Curly braces {} It means a Table structure .
do-end blocks It solves the problem of variable scope , First of all, we must understand local Variables and global variables have completely different lexical scopes ; We should try to state local Variable , Try to reduce the scope of a variable , Make it only exist in the variable block you need , Instead of polluting the wider public environment ;
First, let's refer to two examples :
do
local x1=1
local x2=2
local sum=x1+x2
end
print(x1,x2) --nil nil
No, do end:
local x1=1
local x2=2
local sum=x1+x2
print(x1,x2) --1 2
stay Lua in , Use do-end Wrap one or more statements as a statement block (block), Form a scope , Local variables defined in this scope are only valid in the current scope , After being out of scope, it will be automatically recycled by the garbage collector !
do
local l_var = 1 -- local variable
g_var = 2 -- Global variables
print(l_var, g_var) -- 1 2
end
print(l_var, g_var) -- nil 2
Global variables defined in the scope are not affected by the scope !
边栏推荐
猜你喜欢
Scala higher order (10): exception handling in Scala

Docker最新超详细教程——Docker创建运行MySQL并挂载

Meeting notice of OA project (Query & whether to attend the meeting & feedback details)

Android面试题 | 怎么写一个又好又快的日志库?

Segger's hardware anomaly analysis

QT连接两个qslite数据库报错QSqlQuery::exec: database not open

Amazon cloud assistant applet is coming!

【MYSQL】-【子查询】
![[summer daily question] Luogu p6461 [coci2006-2007 5] trik](/img/bf/c0e03f1bf477730f0b3661b3256d1d.png)
[summer daily question] Luogu p6461 [coci2006-2007 5] trik

2-unified return class dto object
随机推荐
【FPGA教程案例42】图像案例2——通过verilog实现图像二值化处理,通过MATLAB进行辅助验证
[summer daily question] Luogu p7760 [coci2016-2017 5] tuna
Introduction to logback appender
受欢迎的牛 G
5-integrate swagger2
gin abort不能阻止后续代码的问题
Thinkphp6 realizes database backup
状态机dp三维
写点dp
Gin abort cannot prevent subsequent code problems
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
利用C语言巧妙实现棋类游戏——三子棋
蓝桥杯A组选数异或
【暑期每日一题】洛谷 P4414 [COCI2006-2007#2] ABC
PAT甲级 1146 拓扑顺序
SEGGER 的硬件异常 分析
Summer summary (II)
Leetcode 209. subarray with the smallest length (2022.07.28)
国内数字藏品的乱象与未来
I, 28, a tester, was ruthlessly dismissed in October: I want to remind people who are still learning to test