chenshuhong / fullstack

我的全栈路线思维导图以及日常知识记录
MIT License
0 stars 0 forks source link

Javascript各种判断 #7

Open chenshuhong opened 4 years ago

chenshuhong commented 4 years ago

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 commented 4 years ago

JavaScript专题之判断两个对象相等