CWDG论坛-专业魔兽插件's Archiver

cosin0002 发表于 2007-9-3 16:04

不能理解为什么metatable的__index得不到值

[language=lua]-- a
local a = {}

a.__index = function(t, k)
        print("# a.__index : "..k)
        local v = rawget(a, k)
        return v
end
-- b
local b = {}
setmetatable(b, a)

print("getmetatable(b) == a : ["..tostring(getmetatable(b) == a).."]")
print("getmetatable(b).__index : ["..type(getmetatable(b).__index).."]")
print("rawget(getmetatable(b), __index) : ["..type(rawget(getmetatable(b), __index)).."]")
print("rawget(a, __index) : ["..type(rawget(a, __index)).."]")
[/language][quote]getmetatable(b) == a : [true]
getmetatable(b).__index : [function]
rawget(getmetatable(b), __index) : [nil]
rawget(a, __index) : [nil][/quote]
不明白为什么直接访问 a.__index 是有值的,而 rawget(a, __index) 是 nil

simonw 发表于 2007-9-3 20:26

这是一个自我深陷其中难以察觉的低级错误
print(rawget(a, "__index"))

cosin0002 发表于 2007-9-3 20:53

=.= 低级错误。。。

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.