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

Javascript各种判断 #7

Open
chenshuhong opened this issue Aug 23, 2019 · 1 comment
Open

Javascript各种判断 #7

chenshuhong opened this issue Aug 23, 2019 · 1 comment
Labels

Comments

@chenshuhong
Copy link
Owner

chenshuhong commented Aug 23, 2019

JavaScript专题之类型判断(上)
JavaScript专题之类型判断(下)
类型判断

// 第二版
var class2type = {};

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

function type(obj) {
    // 一箭双雕
    if (obj == null) {
        return obj + "";
    }
    return typeof obj === "object" || typeof obj === "function" ?
        class2type[Object.prototype.toString.call(obj)] || "object" :
        typeof obj;
}
@chenshuhong
Copy link
Owner Author

@chenshuhong chenshuhong added the js label Aug 27, 2019
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