当前位置:网站首页>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 !
边栏推荐
- 【暑期每日一题】洛谷 P1601 A+B Problem(高精)
- Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
- 受欢迎的牛 G
- jdbc入门
- 利用C语言巧妙实现棋类游戏——三子棋
- Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
- Paper reading (62):pointer networks
- [summer daily question] Luogu p7760 [coci2016-2017 5] tuna
- Use of gcc/g++
- 【Unity实战100例】Unity万能答题系统之单选多选判断题全部通用
猜你喜欢
随机推荐
Vue router route cache
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
国内数字藏品的乱象与未来
【暑期每日一题】洛谷 P1601 A+B Problem(高精)
log4qt内存泄露问题,heob内存检测工具的使用
Pat class a 1154 vertex shading
Paper reading (62):pointer networks
2-统一返回类DTO对象
Section 7 - compilation of programs (preprocessing operations) + links
3-全局异常处理
logback 中FileAppender具有什么功能呢?
Using C language to skillfully realize the chess game -- Sanzi chess
我想问一下,我flink作业是以upsert-kafka的方式写入数据的,但是我在mysql里面去更
请问flink支持sqlServer数据库么?获取sqlServer数据库的变化
蓝桥杯A组选数异或
QT基础第二天(2)qt基础部件:按钮类,布局类,输出类,输入类,容器等个别举例
I, 28, a tester, was ruthlessly dismissed in October: I want to remind people who are still learning to test
Pat class a 1150 traveling salesman problem
Latest 10 billion quantitative private placement list
do end用法的妙处








