最近疯狂刷面试题,闲来无事,记录一下知识点
判断数据类型的方法(先讲方法,后讲优缺点)
- typeof
- instanceof
- Object.Prototype.toString.call()
- constructor(暂时不做介绍,感兴趣可以自己查阅一下)
- 正则表达式判断(暂时不做介绍,感兴趣可以自己查阅一下)
typeof
基础用法:typeof 变量
或 typeof (变量)
返回值:number
、string
、boolean
、undefined
、symbol
、object
其中之一
简单例子展示:
console.log(typeof 1); //number
console.log(typeof '1'); //string
console.log(typeof true); //boolean
console.log(typeof undefined); //undefined
console.log(typeof Symbol(1)); //symbol
console.log(typeof null); //object
console.log(typeof [1]); //object
console.log(typeof { 1: 1 }); //object
原理扩展
在网上查了一些资料,这个小伙伴写的感觉简单明了,可以去看看typeof原理
缺点
在上述例子中,细心观察就会发现存在问题,typeof是用于区分数据类型的,但是typeof输出null、数组、对象的结果均是object,这就会使得判断这三种类型时会出现问题。
优点
因此根据上述,typeof能用于判断除了null以外的简单数据类型(string、number、boolean、undefined、symbol),这样快速而方便。
小坑
通过typeof转换后返回的都是以字符串的形式返回,因此多次typeof返回的结果必是string
感兴趣的可以猜猜这个的结果typeof typeof typeof typeof [1,2,3]
instanceof
基础用法: 变量 instanceof 类型名称
返回值:true
or false
简单例子展示:
console.log('1' instanceof String); // false
console.log(false instanceof Boolean); // false
console.log(1 instanceof Number); // false
console.log(Symbol(1) instanceof Symbol); // false
console.log(undefined instanceof undefined); // TypeError: Right-hand side of 'instanceof' is not an object
console.log(null instanceof null); // TypeError: Right-hand side of 'instanceof' is not an object
console.log([1] instanceof Array); // false
console.log({ 1: 1 } instanceof Object); // false
原理扩展
instanceof是通过原型链来判断的,instanceof 是用于检测构造函数(右边类型名称)的 prototype 属性是否出现在某个实例对象(左边变量)的原型链上。基于此,简单的例子改造升级一下就会发现不同。
const str = new String('1')
const str1 = '1'
console.log(str instanceof String) // true
console.log(str1 instanceof String) // false
同样的字符串,通过new关键字构造的字符串用instanceof判断则为true,直接声明则为false。
由于通过new关键字构造的变量是存在隐式原型的,而直接声明字符串变量则不存在,因此才会造成同样的字符串会有不一样的结果。
那么简单例子中console.log(undefined instanceof undefined)
为什么会抛出TypeError: Right-hand side of 'instanceof' is not an object
的异常呢?
在instanceof右边的类型名称,需要在原型链上能够找到object,而null、undefined不存在原型,因此抛出异常。
缺点
总结一下instanceof的缺点,instanceof比较难以区分简单数据类型,instanceof最好不要用于判断简单类型,如果实在需要用instanceof判断简单数据类型,则只能采用构造函数的方法new出对应的变量
优点
instanceof 可以区分数组与对象,弥补了typeof的缺点
Object.Prototype.toString.call()
基本用法: Object.Prototype.toString.call(变量)
返回值:[Object 类型名称]
简单举例:
console.log(Object.prototype.toString.call('11111')); // [object String]
console.log(Object.prototype.toString.call(1)); // [object Number]
console.log(Object.prototype.toString.call(false)); // [object Boolean]
console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
console.log(Object.prototype.toString.call(null)); // [object Null]
console.log(Object.prototype.toString.call(Symbol(1))); // [object Symbol]
console.log(Object.prototype.toString.call([1])); // [object Array]
console.log(Object.prototype.toString.call({ 11: 11 })); // [object Object]
原理扩展
每个对象都有一个 toString() 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下,toString() 方法被每个 Object 对象继承。为了每个对象都能通过Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用详细介绍
缺点
Object.prototype.toString()其实是可以被覆盖的,如果被覆盖,那么便不能获取对应的数据类型
优点
当Object.prototype.toString()不被覆盖时,就可以采用这种方式检查所有的数据类型,甚至可以检测Date、Math对象。
判断所有数据类型的方法
Object.prototype.toString()
(不被覆盖的情况下)- 利用typeof判断,如果返回object再采用instanceof(有很多种方法实现,随便写了一种简单点的)
function iTypeof(value) {
if (value === null) {
return 'null'
} else {
if (typeof value === 'object') {
if(value instanceof Array) {
return 'Array'
}
if (value instanceof Object) {
return 'Object'
}
}
return typeof value
}
}
实现instanceof判断所有数据类型
function iInstance(value, type) {
if (typeof value !== 'object' || value === null) {
if (typeof value !== 'function') return false
}
let valueProto = value.__proto__
while(true){
if (valueProto === type.prototype) return true
if (valueProto === null) return false
valueProto = valueProto.__proto__
}
}
以上有问题的话,请各位大佬们评论告诉我一声,苦逼应届生还没找着工作,轻喷!!