美文网首页Web前端之路
JavaScript判断类型

JavaScript判断类型

作者: Funwt | 来源:发表于2017-08-19 16:09 被阅读23次
var class2type = {
    "[object Boolean]": "boolean",
    "[object Number]": "number",
    "[object String]": "string",
    "[object Function]": "function",
    "[object Array]": "array",
    "[object Date]": "date",
    "[object RegExp]": "regexp",
    "[object Object]": "object",
    "[object Error]": "error",
    "[object Symbol]": "symbol"
}
var toString = Object.prototype.toString;

jQuery.type = function (obj) {
    if (obj == null) {
        return obj + "";
    }
    return 
      typeof obj === "object" || typeof obj === "function" ? 
        class2type[toString.call(obj)] || "object" : 
        typeof obj;
}

相关文章

网友评论

    本文标题:JavaScript判断类型

    本文链接:https://www.haomeiwen.com/subject/ohbtdxtx.html