现在的位置: 首页 > 综合 > 正文

lua 的__newindex

2019年08月06日 ⁄ 综合 ⁄ 共 356字 ⁄ 字号 评论关闭

lua的__newindex 有一点没太明白,是不是这么用太无聊了???

 

假如:

 

_t ={}

 

m ={}

 

m.__newindex= function(t,n,k)

print("this is the newindex")

_t[n] = k

end

 

setmetatable(_t,m)

 

_t[2] = 100

 

print(_t[2])

会报错。
要是改成这样:
_t ={}
temp = {}
m ={}
m.__newindex= function(t,n,k)
print("this is the newindex")
temp[n] = k
end
setmetatable(_t,m)
_t[2] = 100
print(_t[2]) -- nil
print (temp[2]) --100
这么是说明__newindex 无法给自身处理吗? 因为本身就可以做赋值吗? 没太明白。
【上篇】
【下篇】

抱歉!评论已关闭.