当前位置:网站首页>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 高阶(十):Scala中的异常处理
- Other basic monitoring items of ZABBIX
- thinkphp6 实现数据库备份
- Use custom annotations to verify the size of the list
- Log4qt memory leak, use of heob memory detection tool
- ef core 读取text类型慢_ef core读取大字符串字段慢
- logback中RollingFileAppender属性简介说明
- 蓝桥杯A组选数异或
- 【暑期每日一题】洛谷 P6336 [COCI2007-2008#2] BIJELE
- 【暑期每日一题】洛谷 P6500 [COCI2010-2011#3] ZBROJ
猜你喜欢
美智光电IPO被终止:年营收9.26亿 何享健为实控人
Thinkphp6 realizes database backup
国内数字藏品的乱象与未来
Use custom annotations to verify the size of the list
Some learning and understanding of vintage analysis
jdbc入门
Scala 高阶(十):Scala中的异常处理
[summer daily question] Luogu p7760 [coci2016-2017 5] tuna
Meizhi optoelectronics' IPO was terminated: annual revenue of 926million he Xiangjian was the actual controller
Scala higher order (IX): pattern matching in Scala
随机推荐
Starting process of raspberry pie
Halcon installation and testing in vs2017, DLL configuration in vs2017
Log4qt memory leak, use of heob memory detection tool
ef core 读取text类型慢_ef core读取大字符串字段慢
树莓派的启动流程
Gin abort cannot prevent subsequent code problems
MySQL如何把行转换为列?
Introduction and introduction of logback
[summer daily question] Luogu p4414 [coci2006-2007 2] ABC
3-global exception handling
Round avatar of user list and follow small blocks
RoBERTa:A Robustly Optimized BERT Pretraining Approach
Getting started with JDBC
程序的静态库与动态库的区别
CDC source can quit after reading MySQL snapshot split
[daily question in summer] Luogu p6408 [coci2008-2009 3] pet
log4qt内存泄露问题,heob内存检测工具的使用
零数科技深度参与信通院隐私计算金融场景标准制定
Job 7.28 file IO and standard IO
Paper reading (62):pointer networks