如何判断js中对象的类型

JavaScript08

如何判断js中对象的类型,第1张

如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较<br><br>如何判断js中的类型呢,先举几个例子:<br><br>var a = "iamstring."<br><br>var b = 222<br><br>var c= [1,2,3]<br><br>var d = new Date()<br><br>var e =<br>function(){alert(111)}<br><br>var f =<br>function(){this.name="22"}<br><br>最常见的判断方法:typeof<br><br>alert(typeof a)<br> ------------>string<br><br>alert(typeof b)<br> ------------>number<br><br>alert(typeof c)<br> ------------>object<br><br>alert(typeof d)<br> ------------>object<br><br>alert(typeof e)<br> ------------>function<br><br>alert(typeof f)<br> ------------>function<br><br>其中typeof返回的类型都是字符串形式,需注意,例如:<br><br>alert(typeof a == "string")<br>------------->true<br><br>alert(typeof a == String)<br>--------------->false<br><br>另外typeof<br>可以判断function的类型;在判断除Object类型的对象时比较方便。<br><br>判断已知对象类型的方法: instanceof<br><br>alert(c instanceof Array)<br>--------------->true<br><br>alert(d instanceof<br>Date) <br><br>alert(f instanceof Function)<br>------------>true<br><br>alert(f instanceof function)<br>------------>false<br><br>注意:instanceof<br>后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。<br><br>根据对象的constructor判断:<br>constructor<br><br>alert(c.constructor ===<br>Array) ---------->true<br><br>alert(d.constructor === Date)<br>----------->true<br><br>alert(e.constructor ===<br>Function) ------->true<br><br>注意: constructor 在类继承时会出错<br><br>eg,<br><br>function A(){}<br><br>function B(){}<br><br>A.prototype = new B()//A继承自B<br><br>var aObj = new A()<br><br>alert(aobj.constructor === B) -----------><br>true<br><br>alert(aobj.constructor === A) -----------><br>false<br><br>而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:<br><br>alert(aobj instanceof B) ----------------><br>true<br><br>alert(aobj instanceof B) ----------------><br>true<br><br>言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:<br><br>aobj.constructor = A<br>//将自己的类赋值给对象的constructor属性<br><br>alert(aobj.constructor === A) -----------><br>true<br><br>alert(aobj.constructor === B) -----------><br>false//基类不会报true了<br><br>通用但很繁琐的方法: prototype<br><br>alert(Object.prototype.toString.call(a) === ‘[object String]’)<br>------->true<br><br>alert(Object.prototype.toString.call(b) === ‘[object Number]’)<br>------->true<br><br>alert(Object.prototype.toString.call(c) === ‘[object Array]’)<br>------->true<br><br>alert(Object.prototype.toString.call(d) === ‘[object Date]’)<br>------->true<br><br>alert(Object.prototype.toString.call(e) === ‘[object Function]’)<br>------->true<br><br>alert(Object.prototype.toString.call(f) === ‘[object Function]’)<br>------->true<br><br>大小写不能写错,比较麻烦,但胜在通用。<br><br>通常情况下用typeof<br>判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,简单总结下,挖个坑,欢迎补充!

在前端开发中我们经用到的操作有很多,比如判断数据类型、去重、深拷贝等等,最近也在整理常用的知识点,便于积累和后期查看,这里呢我对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方法比较<br><br>如何判断js中的类型呢,先举几个例子:<br><br>var a = "iamstring."<br><br>var b = 222<br><br>var c= [1,2,3]<br><br>var d = new Date()<br><br>var e =<br>function(){alert(111)}<br><br>var f =<br>function(){this.name="22"}<br><br>最常见的判断方法:typeof<br><br>alert(typeof a)<br>------------>string<br><br>alert(typeof b)<br>------------>number<br><br>alert(typeof c)<br>------------>object<br><br>alert(typeof d)<br>------------>object<br><br>alert(typeof e)<br>------------>function<br><br>alert(typeof f)<br>------------>function<br><br>其中typeof返回的类型都是字符串形式,需注意,例如:<br><br>alert(typeof a == "string")<br>------------->true<br><br>alert(typeof a == String)<br>--------------->false<br><br>另外typeof<br>可以判断function的类型;在判断除Object类型的对象时比较方便。<br><br>判断已知对象类型的方法: instanceof<br><br>alert(c instanceof Array)<br>--------------->true<br><br>alert(d instanceof<br>Date) <br><br>alert(f instanceof Function)<br>------------>true<br><br>alert(f instanceof function)<br>------------>false<br><br>注意:instanceof<br>后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。<br><br>根据对象的constructor判断:<br>constructor<br><br>alert(c.constructor ===<br>Array) ---------->true<br><br>alert(d.constructor === Date)<br>----------->true<br><br>alert(e.constructor ===<br>Function) ------->true<br><br>注意: constructor 在类继承时会出错<br><br>eg,<br><br>function A(){}<br><br>function B(){}<br><br>A.prototype = new B()//A继承自B<br><br>var aObj = new A()<br><br>alert(aobj.constructor === B) -----------><br>true<br><br>alert(aobj.constructor === A) -----------><br>false<br><br>而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:<br><br>alert(aobj instanceof B) ----------------><br>true<br><br>alert(aobj instanceof B) ----------------><br>true<br><br>言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:<br><br>aobj.constructor = A<br>//将自己的类赋值给对象的constructor属性<br><br>alert(aobj.constructor === A) -----------><br>true<br><br>alert(aobj.constructor === B) -----------><br>false//基类不会报true了<br><br>通用但很繁琐的方法: prototype<br><br>alert(Object.prototype.toString.call(a) === ‘[object String]’)<br>------->true<br><br>alert(Object.prototype.toString.call(b) === ‘[object Number]’)<br>------->true<br><br>alert(Object.prototype.toString.call(c) === ‘[object Array]’)<br>------->true<br><br>alert(Object.prototype.toString.call(d) === ‘[object Date]’)<br>------->true<br><br>alert(Object.prototype.toString.call(e) === ‘[object Function]’)<br>------->true<br><br>alert(Object.prototype.toString.call(f) === ‘[object Function]’)<br>------->true<br><br>大小写不能写错,比较麻烦,但胜在通用。<br><br>通常情况下用typeof<br>判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,简单总结下,挖个坑,欢迎补充!