// typeof 只能检测基础数据类型null除外 对与复杂引用类型 function除外,
typeof 1 // "unmber"
typeof undefined // "undefined"
typeof Symbol // "Symbol"
typeof [] // "object"
typeof {} // "object"
typeof unll // "object" 如果真需要判断是否为null 只需 使用 === 判断既可
typeof console // "object"
typeof console.log // "function"
// instanceof 该方法用于检测属性、方法是否包含在原型上可以检测复杂的引用类型属性,但无法准确判断基础数据类型
let Car = functon() {}
let benz = new Car()
benz instanceof Car // true
let car = new String("Mercedes Benz")
car instanceof String // true
let str = "Covid-19"
str instanceof String // false
// Object.prototype.toString.call()
function getType(obj) {
let type = typeof obj;
if(type !== "object") {
return type
}
return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/,"$1")
}
getType([]) // "Array"
getType("str") // "String"
getType(1) // "Number"