示例工程 05-variables-and-scope

05-variables-and-scope/02-local-x-equals-x/README.md

markdown · 0.7 KB

# 02 · local-x-equals-x
 
**演示**:`local x = x` 的左侧新变量直到语句结束才可见,右侧读外层;没有外层同名变量时读到全局 `nil`。
**运行时**:`lua54`、`luajit`。
**运行**:
 
```sh
.tools/bin/lua54 scripts/run-example.lua examples/05-variables-and-scope/02-local-x-equals-x
.tools/bin/lua54 scripts/test-example.lua examples/05-variables-and-scope/02-local-x-equals-x
```
 
**预期输出**(lua 5.4.9 / macOS arm64):
 
```text
after=11
fresh_nil=true
```
 
**看点**:同名声明不是「重新赋值」,而是新变量。
**注意**:想复用外层值请换名,别依赖这个细节。
 
**luacheck 局部放行**:`local x = x` 的重定义是本例重点。