计算机typeof检测,返回的是对应的数据类型,机器编码决定数据类型
null会返回为object是因为null机器编码是00000...000 => object
console.log(typeof(123)) //number
console.log(typeof('123')) //string
console.log(typeof(true)) //boolean
console.log(typeof(undefined))//undefined
console.log(typeof(Object)) //function
console.log(typeof(null)) //object
console.log(typeof(()=>{})) //function
typeof引用类型返回定义一个[[call]]:决定是返回object还是返回function
来看一道面试题
请告诉我你的答案是什么
var str = 'strs'
console.log('---------------')
console.log(str)
console.log(typeof('strs'))
console.log('---------------')
console.log('---------------')
var str1 = new String('strs')
console.log(str1)
console.log(typeof(str1))
console.log('---------------')
instanceof检测布尔值
instanceof 原型链 A instanceof B true , B instanceof C true
通过原型去查找
console.log([] instanceof Array); //true
console.log({} instanceof Object); //true
console.log(new Date() instanceof Date); //true
function Person(){}; //true
console.log(new Person instanceof Person); //true
console.log([] instanceof Object); //true
console.log(new Date() instanceof Object); //true
console.log(new Person instanceof Object); //true
开发中常用的方式
console.log(Object.prototype.toString.call('检测数据类型'));