判断数据类型的方法
JS中常见的几种数据类型:
- 基本数据类型:字符串(String)、数字(Number)、布尔(Boolean)、空值(Null)、未定义(Undefined)、Symbol、BigInt
- 引用数据类型:对象(Object)、数组(Array)、函数(Function)
typeof
常见方法。一般用来判断基本数据类型,如:string、number、boolean、symbol、bigint(es10新增一种基本数据类型bigint,具体介绍)、undefined等。
typeof
目前能返回string、number、boolean、symbol、bigint、undefined、object、function等。
typeof 'str' string
typeof 1 number
typeof NaN number
typeof true boolean
typeof Symbol('1') symbol
typeof 111n bigint
typeof undefined undefined
typeof null object
typeof {a:1,b:2} object
typeof [1,2] object
typeof new Date() object
typeof new RegExp() object
typeof function s(){} function
⚠️:
null既然属于基本数据类型,为什么用typeof
返回的是object?
js在底层存储变量的时候,会在变量的机器码的低位存储其类型信息
000:对象
010:浮点数
100:字符串
110:布尔
1:整数
null:所有机器码均为0
undefined:用-2^30整数来表示
instanceof
一般用来判断引用数据类型的判断,如:Object、Function、Array、Date、RegExp等。
[] instanceof Array true
[] instanceof Object true
/s/g instanceof RegExp true
new Date('2022/03/14') instanceof Date true
[1,2,3] instanceof Array true
instanceof
主要的作用就是判断一个实例是否属于某种类型,例如:
let animal = function () {}
let cat = new animal()
cat instanceof animal true
instanceof
可以判断一个实例是否是其父类型或者祖先类型的实例。
let person = function() {}
let programmer = function() {}
programmer.prototype = new person()
let str = new programmer()
str instanceof person true
str instanceof programmer true
可以用以下代码实现instanceof
:
function instance(left,right){
let rightProto = right.prototype;//取右表达式的prototype值
let proto = left._proto_;//取左表达式的_proto_值
while(true){
//为什么有proto===null这一层判断,因为null为原型链的终点
//原型链最终都指向null,所有当proto为null时,说明已经遍历完原型链上的所有原型对象,结果是:right不是left的构造函数
if(proto === null){
return false
}
if(proto === rightProto){
return true
}
proto = proto._proto_
}
}
⚠️:
其实instanceof主要的实现原理就是只要右边变量的prototype在左边变量的原型链上即可。因此,instanceof在查找的过程中会遍历左边变量的原型链,直到找到右边变量的prototype,如果查找失败,则会返回false,告诉我们左边变量并非是右边变量的实例。
construtor
当一个函数F被定义时,JS引擎会为F添加prototype原型,然后再在prototype上添加一个constructor属性,并让其指向F的引用。如下所示:
当执行var f=new F()时,F被当成了构造函数,f是F的实例对象,此时F原型上的constructor传递到了f上,因此f.constructor=F
可以看出,JS在函数F的原型上定义了constructor,当F被当作构造函数用来创建对象时,创建的新对象就被标记为了“F”类型,使得新对象有名有姓。可以追溯。
同理,JS中的数据类型也遵守这个规则:
''.constructor===String
true
new Number(1).constructor==Number
true
true.constructor==Boolean
true
new Function().constructor==Function
true
new Date().constructor==Date
true
new Error().constructor==Error
true
[].constructor==Array
true
document.constructor==HTMLDocument
true
window.constructor==Window
true
⚠️:
- null和undefined是无效的对象,因此是不会有constructor存在的,这两种类型可以通过第四种方法来判断
- JS对象的constructor是不稳定的,主要体现在自定义对象上,当开发者重写prototype后,原有的constructor会丢失,constructor会默认为Object
Object.prototype.toString
(这个是判断类型最准的方法)
toString是Object原型对象上的一个方法,该方法默认返回其调用者的具体类型,更严格的讲,是toString运行时this指向的对象类型,返回的类型格式为[object xxx],xxx是具体的数据类型,其中包括:String、Number、Boolean、Undefined、Null、Function、Date、Array、RegExp、Error、HTMLDocument、…基本上所有对象的类型都可以通过这个方法获取到。
Object.prototype.toString.call('') [object String]
Object.prototype.toString.call(1) [object Number]
Object.prototype.toString.call(true) [object Boolean]
Object.prototype.toString.call(undefined) [object Undefined]
Object.prototype.toString.call(null) [object Null]
Object.prototype.toString.call(new Function()) [object Function]
Object.prototype.toString.call(new Date()) [object Date]
Object.prototype.toString.call([]) [object Array]
Object.prototype.toString.call(new RegExp()) [object RegExp]
Object.prototype.toString.call(new Error()) [object Error]
Object.prototype.toString.call(document) [object HTMLDocument]
Object.prototype.toString.call(window) [object global] //window是全局对象global
⚠️:
必须通过Object.prototype.toString.call来获取,而不是直接new Date().toString(),从原型链的角度讲,所以对象的原型链最终都指向了Object,按照JS变量查找规则,其他对象应该也可以直接访问到Object的toString方法,而事实上,大部分的对象都实现了自身的toString方法,这样就可能会导致Object的toString被终止查找,因此要来call来强制执行Object的toString方法。