其他分享
首页 > 其他分享> > xlua _newIndex 探究

xlua _newIndex 探究

作者:互联网

1 指向table

t = {}
f = {key = "hjj"}
p = setmetatable(t, { __newindex = f })


p.key1 = 2

print(t.key1)
print(f.key1)
print(f.key)

-----输出----
nil 
2
hjj


---等价代码
t = {}
f = {key = "hjj"}
p = setmetatable(t, { __newindex =
		function(table, key, value)
			f[key] = value
		end})


p.key1 = 2

print(f.key1)

2  指向函数

t = {}
f = {key = "hjj"}
p = setmetatable(t, { __newindex =
		function(table, key, value)
			print("不存在数据")
			print(key)
			print(value)
			print(table)
		end})


p.key1 = 2


-----输出
不存在数据
key1
2
table: 0x56380445b290

3 构造类多继承

标签:key1,xlua,探究,value,key,print,table,newIndex,hjj
来源: https://blog.csdn.net/hjssss/article/details/100141378