TypeOf

lua-users home
wiki

繞著 typeof() 運算符的概念玩。

當使用 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
    

最近變更 · 偏好設定
編輯 · 歷程記錄
最後編輯時間為 2006 年 3 月 1 日, 晚上 11:47 GMT (差異)