如何判断js中对象的类型

JavaScript029

如何判断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中的数据类型: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方法,简单总结下,挖个坑,欢迎补充!