Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js类型判断 #62

Open
wuyunqiang opened this issue Jun 21, 2018 · 0 comments
Open

js类型判断 #62

wuyunqiang opened this issue Jun 21, 2018 · 0 comments
Labels

Comments

@wuyunqiang
Copy link
Owner

       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;
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant