首页 > 代码库 > Lua学习笔记(weak table)

Lua学习笔记(weak table)

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

 

Lua学习笔记(weak table)