当前位置:网站首页>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 !
边栏推荐
- [summer daily question] Luogu p6320 [coci2006-2007 4] sibice
- QT basic day 2 (2) QT basic components: button class, layout class, output class, input class, container and other individual examples
- [OpenGL] use of shaders
- I, 28, a tester, was ruthlessly dismissed in October: I want to remind people who are still learning to test
- Practice of online problem feedback module (XVII): realize the online download function of excel template
- 女研究生做“思维导图”与男友吵架!网友:吵架届的“内卷之王”....
- JS day 4 process control (if statement and switch statement)
- STM32 operation w25q256 w25q16 SPI flash
- Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
- logback中RollingFileAppender属性简介说明
猜你喜欢

1 - background project construction

2022年深圳杯A题破除“尖叫效应”与“回声室效应”走出“信息茧房”

How does MySQL convert rows to columns?

1-后台项目搭建

2-统一返回类DTO对象

Use of gcc/g++

NFT 的 10 种实际用途

Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)

Getting started with JDBC

Using C language to skillfully realize the chess game -- Sanzi chess
随机推荐
【暑期每日一题】洛谷 P4414 [COCI2006-2007#2] ABC
js第四天流程控制(if语句和switch语句)
logback日志级别简介说明
【WPF】通过动态/静态资源实现语言切换
Segger's hardware anomaly analysis
梳理市面上的2大NFT定价范式和4种解决方案
[summer daily question] Luogu p1601 a+b problem (high precision)
能在SQL 语句中 指定 内存参数吗?
女研究生做“思维导图”与男友吵架!网友:吵架届的“内卷之王”....
logback 中FileAppender具有什么功能呢?
Some learning and understanding of vintage analysis
状态机dp(简单版)
thinkphp6 实现数据库备份
Levelfilter introduction
QT专题:基础部件(按钮类,布局类,输出类,输入类,容器类)
我,28岁,测试员,10月无情被辞:想给还在学测试 的人提个醒......
Prometheus与Grafana
How to establish EDI connection with Scania in Scania?
UPC 小C的王者峡谷
亚马逊云助手小程序来啦!