【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));

 

posted @ 2021-11-18 13:47  木子欢儿  阅读(38)  评论(0编辑  收藏  举报