typeof 和 instanceof区别

typeof

返回值结果:“number”、“string”、“boolean”、“object”、“function”、“undefined”、“symbol”

若参数为引用类型,始终返回“object”,对于Array、null都返回“object”

typeof 1 // 'number'
typeof '1' // 'string'
typeof undefined // 'undefined'
typeof true // 'boolean'
typeof Symbol() // 'symbol'

typeof [] //'object'
typeof {} // 'object'
typeof null // 'object'
typeof console.log //'function'

instanceof

instanceof是用来判断一个对象在其原型链中是否存在相应的构造函数

// 判断a是否为b的实例,b是否为a的构造函数
a instanceof b

 

posted @ 2020-04-10 13:24  seeBetter  阅读(158)  评论(0编辑  收藏  举报