js判断数据类型方法汇总

JavaScript013

js判断数据类型方法汇总,第1张

在前端开发中我们经用到的操作有很多,比如判断数据类型、去重、深拷贝等等,最近也在整理常用的知识点,便于积累和后期查看,这里呢我对js中数据类型判断方法以及判断结果进行了汇总。

一、汇总表格

二、4种方式说明

1、typeof

对于原始类型:除了null其它都可以显示正确

对于对象的话:除了function  其它均显示为 “object”

2、 instanceof : 内部机制是通过原型链来判断的  方法是  a instanceof b  (a是不是b的实例)

针对于对象:可以很明显的区分Array、Date、regExp,但是他们都是Object的实例。所以,instanceof 最好是用来判断两个对象是否属于实例关系, 而不是判断一个对象实例具体属于哪种类型。

3、constructor a.constructor===Function / Symbol / String / Number / Boolean / Object / RegExp / Date

对于原始类型:无法处理null、undefined(这两个会报错) 

对于对象:均可以判断

函数的 constructor 是不稳定的,这个主要体现在自定义对象上,当开发者重写 prototype 后,原有的 constructor 引用会丢失,constructor 会默认为 Object

4、 toString

toString() 是 Object 的原型方法,调用该方法,默认返回当前对象的 [[Class]] 。这是一个内部属性,其格式为 [object Xxx] ,其中 Xxx 就是对象的类型。

对于 Object 对象,直接调用 toString()  就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。

可以判断所有类型: Object.prototype.toString.call(xxx)  对向的话可以直接使用 Object.toString(obj)

三、整理一个可以判断任意数据类型的方法

注意:在es6中用class定义类的时候,通过typeof判断出的结果是Function,而通过Object.toString判断的结果是Object。js中class应该是Function类型,所以这点需要注意。

function getType(para) {  //判断任意数据类型

    const type = typeof para

    if (type === "number" && isNaN(para)) return "NaN"

    if (type !== "object") return type

    return Object.prototype.toString

        .call(para)

        .replace(/[\[\]]/g, "")

        .split(" ")[1]

        .toLowerCase()

}

四、小结

js中数据类型判断的方式有4种:typeof、instance、constructor、toString,typeof简单方便,比较适合原始类型判断,toString繁琐一点但是判断全面,所以这两个的结合判断我是比较推荐的。

在 ES5 中,存在 6 种数据类型。

基本类型(值类型): String、Number、Boolean、undefined、null

对象类型(引用类型):Object,其中 Object 有三种特殊的对象(Function、Array、Date)

PS: undefined 和 null 的第一个字母都是小写

Q: undefined 和 null 有什么区别?

A: 当一个变量被声明,但是还没有赋值的时候,则为 undefined.

当一个变量被声明了,并且已经赋值了,赋的值为 null,则为 null

根本区别就是在于在声明的时候有没有被赋值

常用的判断数据类型的方法

划重点!!! typeof(xxx) 返回的是一个字符串

举个栗子1

举个栗子2

举个栗子3

typeof 可以判断以上 4 中基本类型(Number、String、Boolean、undefined)

判断不了 null 类型

举个栗子4

是不是很奇怪?null 作为基础类型,但是在判断类型的时候却为 object?

其实设计者是这么想的,他先定义了一个变量,这个变量是准备赋值为对象的,由于某些原因,比如对象的属性还不清楚,所以一开始就给变量赋值为 null,表明这个变量将来是个对象。

null 还有另外一个作用,在最后的时候,给变量赋值为 null,可以让变量指向的对象成为垃圾对象,从而被垃圾回收器回收。

上面的例子证明,null 类型和 对象类型通过 typeof 是无法区分的。

没错,JS 的开发者就是不想让你们这么好过,只记住一个规矩就想闯天下了吗??? Naive!!!

为了让你们多学点本领,于是就有了 instanceof

instanceof 字面意思就是实例。a instanceof b, a 是 b 的实例,b 为 构造函数 。返回值为 布尔值

实现原理是通过检测 b.prototype 是否存在于 a 的原型链上

举个栗子1

但是吧,你想用来判断 null 类型,不好意思,直接报错, null 并不是一个对象

先看完下面的例子,再来看怎么判断 null

下面看个有迷惑行为的例子

言归正传,怎么说来说去都没说怎么判断是不是 null 类型

===

直接上全等于 === 不就好了嘛,就是这么简单快捷!!!

typeof:

可以判断除了 null 之外的值类型 Number、String、Boolean、undefined

还可以判断一个引用类型 Function

不能区分: null 和 Object

instanceof:

判断对象的具体类型

===

判断 undefined 和 null

一般情况下,使用 typeof 去判断就可以了。

当确定是 Object 类型的数据,则使用 instanceof 去具体区分是属于 Function/Array/Date 的哪种类型

很多情况下,都要判断数据不能为 undefined 和 null,那就可以直接判断 xxx !== undefined &&&xxx !== null

最常见的判断方法:typeof

alert(typeof a) ------------>string

alert(typeof b) ------------>number

alert(typeof c) ------------>object

alert(typeof d) ------------>object

alert(typeof e) ------------>function

alert(typeof f) ------------>function

其中typeof返回的类型都是字符串形式,需注意,例如:

alert(typeof a == "string") ------------->true

alert(typeof a == String) --------------->false

另外typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。

判断已知对象类型的方法: instanceof

alert(c instanceof Array) --------------->true

alert(d instanceof Date)

alert(f instanceof Function) ------------>true

alert(f instanceof function) ------------>false

注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

根据对象的constructor判断: constructor

alert(c.constructor === Array) ---------->true

alert(d.constructor === Date) ----------->true

alert(e.constructor === Function) ------->true

注意: constructor 在类继承时会出错

eg,

function A(){}

function B(){}

A.prototype = new B()//A继承自B

var aObj = new A()

alert(aobj.constructor === B) ----------->true

alert(aobj.constructor === A) ----------->false

而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:

alert(aobj instanceof B) ---------------->true

alert(aobj instanceof B) ---------------->true

言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:

aobj.constructor = A//将自己的类赋值给对象的constructor属性

alert(aobj.constructor === A) ----------->true

alert(aobj.constructor === B) ----------->false//基类不会报true了

通用但很繁琐的方法: prototype

alert(Object.prototype.toString.call(a) === ‘[object String]’) ------->true

alert(Object.prototype.toString.call(b) === ‘[object Number]’) ------->true

alert(Object.prototype.toString.call(c) === ‘[object Array]’) ------->true

alert(Object.prototype.toString.call(d) === ‘[object Date]’) ------->true

alert(Object.prototype.toString.call(e) === ‘[object Function]’) ------->true

alert(Object.prototype.toString.call(f) === ‘[object Function]’) ------->true