Skip to content

js类型判断 #62

Description

@wuyunqiang
       static JsType(obj) {
            let class2type = {};
            // 生成class2type映射
            "Boolean Number String Function Array Date RegExp Object Error".split(" ").map(function(item, index) {
                class2type["[object " + item + "]"] = item.toLowerCase();
            })

            // 一箭双雕 undefined null 值相等 类型不等
            if (obj == null) {
                return obj + "";
            }

           // 优先ES6方法判断数组类型
            if(Array.isArray(obj)){
                return "array"
            }

            return typeof obj === "object" || typeof obj === "function" ?
                class2type[Object.prototype.toString.call(obj)] || "object" :
                typeof obj;
        }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions