type of
type of 操作符返回一个字符串,表示未经计算的操作值的类型
使用方法:
typeof operand
typeof(operand)
operand 表示对象或原始值的表达式,其类型将被返回.
typeof 1 // 'number'
typeof '1' // 'string'
typeof undefined // 'undefined'
typeof true // 'boolean'
typeof Symbol() // 'symbol'
typeof null // 'object'
typeof [] // 'object'
typeof {} // 'object'
typeof console // 'object'
typeof console.log // 'function'
前6个都是基础数据类型,虽然typeof null
为 object
,但这只是 JavaScript
存在的一个悠久 Bug
,不代表null
就是引用数据类型,并且null
本身也不是对象,null
在 typeof
之后返回的是有问题的结果,不能作为判断null
的方法
引用类型数据,用 typeof
来判断的话,除了function
会被识别出来之外,其余的都输出object
想要判断一个变量是否存在,可以使用typeof
:(不能使用if(a)
, 若a
未声明,则报错)
if(typeof a != 'undefined'){
//变量存在
}
instance of
instance of 运算符用于检验构造函数的 prototype 属性是否出现在某个实例对象的原型链上
使用方法:
object instanceof constructor
object 为实例对象,constructor 为构造函数
构造函数通过 new
可以创建一个实例对象,instanceof
能判断这个实例对象是否是之前那个构造函数生成的对象
// 定义构建函数
let Car = function() {}
// 创建实例对象
let benz = new Car()
benz instanceof Car // true
let car = new String('xxx')
car instanceof String // true
let str = 'xxx'
str instanceof String // false
顺着原型链去找,直到找到相同的原型对象,返回true
,否则为false
区别
type of 与 instance of 都是判断数据类型的方法,区别如下:
- type of 会返回一个变量的基础数据类型,而 instance of 返回一个布尔值
- instance of 可以精准地判断复杂引用数据类型,但不能正确判断基础数据类型
- type of 虽然可以判断基础数据类型( null 除外),但是引用数据类型中,除了 function 类型以外,其他的也无法判断
Object.prototype.toString.call()
可以看到,上述两种方法都有弊端,并不能满足所有场景的需求
如果需要通用检测数据类型,可以采用 Object.prototype.toString.call()
,调用该方法,统一返回格式 "[object Xxx]
"
的字符串
如下:
Object.prototype.toString({}) // "[object Object]"
Object.prototype.toString.call({}) // 同上结果,加上call也ok
Object.prototype.toString.call(1) // "[object Number]"
Object.prototype.toString.call('1') // "[object String]"
Object.prototype.toString.call(true) // "[object Boolean]"
Object.prototype.toString.call(function(){}) // "[object Function]"
Object.prototype.toString.call(null) //"[object Null]"
Object.prototype.toString.call(undefined) //"[object Undefined]"
Object.prototype.toString.call(/123/g) //"[object RegExp]"
Object.prototype.toString.call(new Date()) //"[object Date]"
Object.prototype.toString.call([]) //"[object Array]"
Object.prototype.toString.call(document) //"[object HTMLDocument]"
Object.prototype.toString.call(window) //"[object Window]"
//不加.call(),结果都为 "[object object]"
实现一个全局通用的数据类型判断方法:
function getType(obj){
let type = typeof obj;
if (type !== "object") { // 先进行typeof判断,如果是基础数据类型,直接返回
return type;
}
// 对于typeof返回结果是object的,再进行如下的判断,正则返回结果
return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1');
}
使用如下:
getType([]) // "Array" typeof []是object,因此toString返回
getType('123') // "string" typeof 直接返回
getType(window) // "Window" toString返回
getType(null) // "Null"首字母大写,typeof null是object,需toString来判断
getType(undefined) // "undefined" typeof 直接返回
getType() // "undefined" typeof 直接返回
getType(function(){}) // "function" typeof能判断,因此首字母小写
getType(/123/g) //"RegExp" toString返回