tableA = {}
tableB = {}
setmetatable(tableA ,tableB )
tableB .__mode = "k" -- tableB .__mode = "v" 或者tableB .__mode = "kv"
这样,tableA将是一个weak table
当tableA中的键的引用为0,或者对应键的值的引用为0是,就会删除正行属性:
testfunction()
local key = {}
tableA[key] = 1
local key = {}
tableA[key] = 2
end
collectgarbage();
for k,v in pairs(tableA) do
print(v) -- 2
end
时间: 2024-10-08 08:21:10