lua的函数的环境和变量

来源:互联网 发布:网络监控安装步骤图解 编辑:程序博客网 时间:2024/05/01 08:46
--[[
Returns the current environment in use by the function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling getfenv.
If the given function is not a Lua function, or if f is 0, getfenv returns the global environment. The default for f is 1.
]]


f = 4
function a()
    f = "a"
    print( getfenv(b).f, getfenv(0).f, getfenv(1).f, getfenv(2).f, getfenv(3).f)
end
A = {}
setmetatable(A, {__index = _G})
setfenv(a, A)


--print(getfenv(a).f)


function b()
    f = "b"
    A.a()
end
B = {}
setmetatable(B, {__index = _G})
setfenv(b, B)


function c()
    f = "c"
    B.b()
end
C = {}
setmetatable(C, {__index = _G})
setfenv(c, C)
c()


print(f, A.f, B.f, C.f )
原创粉丝点击