当前位置:网站首页>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 !
边栏推荐
- 利用C语言巧妙实现棋类游戏——三子棋
- 使用自定义注解校验list的大小
- 小D的刺绣
- 如何与斯堪尼亚SCANIA建立EDI连接?
- Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
- Introduction to logback appender
- 女研究生做“思维导图”与男友吵架!网友:吵架届的“内卷之王”....
- How to establish EDI connection with Scania in Scania?
- Docker最新超详细教程——Docker创建运行MySQL并挂载
- 2-统一返回类DTO对象
猜你喜欢
Scala higher order (10): exception handling in Scala

零数科技深度参与信通院隐私计算金融场景标准制定

【WPF】通过动态/静态资源实现语言切换

Halcon installation and testing in vs2017, DLL configuration in vs2017

Practice of online problem feedback module (XVII): realize the online download function of excel template
![[MySQL] - [subquery]](/img/81/0880f798f0f41724fd485ae82d142d.png)
[MySQL] - [subquery]

STM32 operation w25q256 w25q16 SPI flash

halcon的安装以及在vs2017中测试,vs2017中dll的配置

Synchronous / asynchronous, blocking / non blocking and IO

JS break and continue and return keywords
随机推荐
logback日志级别简介说明
[summer daily question] Luogu p6320 [coci2006-2007 4] sibice
Scala 高阶(九):Scala中的模式匹配
新生代公链再攻「不可能三角」
jdbc入门
BeanUtils.setProperty()
js第四天流程控制(if语句和switch语句)
利用C语言巧妙实现棋类游戏——三子棋
【MYSQL】-【子查询】
Levelfilter introduction
【WPF】通过动态/静态资源实现语言切换
[MySQL] - [subquery]
Prometheus and grafana
零数科技深度参与信通院隐私计算金融场景标准制定
[summer daily question] Luogu p6336 [coci2007-2008 2] bijele
使用自定义注解校验list的大小
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
Log4qt memory leak, use of heob memory detection tool
受欢迎的牛 G
PAT甲级 1154 顶点着色