js如何判断类型?

JavaScript016

js如何判断类型?,第1张

typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型。返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 7 种:number、boolean、symbol、string、object、undefined、function 等。

有些时候,typeof 操作符会返回一些令人迷惑但技术上却正确的值:

对于基本类型,除 null 以外,均可以返回正确的结果。

对于引用类型,除 function 以外,一律返回 object 类型。

对于 null ,返回 object 类型。

对于 function 返回  function 类型。

其中,null 有属于自己的数据类型 Null , 引用类型中的 数组、日期、正则 也都有属于自己的具体类型,而 typeof 对于这些类型的处理,只返回了处于其原型链最顶端的 Object 类型,没有错,但不是我们想要的结果。

在前端开发中我们经用到的操作有很多,比如判断数据类型、去重、深拷贝等等,最近也在整理常用的知识点,便于积累和后期查看,这里呢我对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繁琐一点但是判断全面,所以这两个的结合判断我是比较推荐的。

如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较

如何判断js中的类型呢,先举几个例子:

var a = "iamstring."

var b = 222

var c= [1,2,3]

var d = new Date()

var e =

function(){alert(111)}

var f =

function(){this.name="22"}

最常见的判断方法: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

大小写不能写错,比较麻烦,但胜在通用。

通常情况下用typeof

判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法