类型 |
|
当使用 module() 函数创建模块时,它会将模块名称的值添加为 metatable._NAME。此函数利用了这种默认行为。注意:这并不适用于 C 扩展,除非作者专门添加了该值。
function typeof(var)
local _type = type(var);
if(_type ~= "table" and _type ~= "userdata") then
return _type;
end
local _meta = getmetatable(var);
if(_meta ~= nil and _meta._NAME ~= nil) then
return _meta._NAME;
else
return _type;
end
end