At the begining of some lua package files, sometimes there will be the line local base = _G
or local base = ...
.
- What's the benefits for doing this?
- What's the differences between these two lines?
At the begining of some lua package files, sometimes there will be the line local base = _G
or local base = ...
.
For the first question, you can refer: Why make global Lua functions local?
For your second one,
What's the differences between these two lines?
When you do local base = _G
, you are assigning base
to be a synonym for the global environment table. On the other hand, in the statement local base = ...
; the ...
refer to vararg
feature of lua.
It can be shown in better detail with the following example:
local a = {...}
for k, v in pairs(a) do print(k, v) end
and then, executing it as follows:
─$ lua temp.lua some thing is passed "here within quotes"
1 some
2 thing
3 is
4 passed
5 here within quotes
As you see, ...
is just a list of arguments passed to the program. Now, when you have
local base = ...
lua assigns the first argument to the variable base
. All other parameters will be ignored in the above statement.