【JavaScript】检测数据类型函数

//安全检测Js基本数据类型和内置对象

function typeOf(o) {
    let _toString = Object.prototype.toString();
    let _type = {
        "undefined": "undefined",
        "number": "number",
        "boolean": "boolean",
        "string": "string",
        "[object Function]": "function",
        "[object RegExp]": "regexp",
        "[object Array]": "array",
        "[object Date]": "date",
        "[object Error]": "error"
    }
    return _type[typeof o] || _type[_toString.call(o)] || (o ? "object" : "null");
}

let a = Math.abs();
document.write(typeOf(a));

本文来自博客园,作者:木子欢儿,转载请注明原文链接:https://www.cnblogs.com/HGNET/p/15571989.html

原文地址:https://www.cnblogs.com/HGNET/p/15571989.html