- table类型是引用传递,直接使用=赋值会导致两个table都指向同一个引用,修改其中一个的元素就会影响到另外一个,此时应当使用clone获得其拷贝:
function clone(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end

被折叠的 条评论
为什么被折叠?



