js 数据类型

一、JS中数据类型

基本数据类型(Undefined、Null、Boolean、Number、String)

复杂数据类型 (Object)

二、判断数据类型

下面将对如下数据进行判断它们的类型

varbool =truevarnum =1varstr ='abc'varund = undefinedvarnul =nullvararr = [1,2,3]varobj = {name:'haoxl',age:18}varfun=function(){console.log('I am a function')}

1.使用typeof

console.log(typeofbool);//booleanconsole.log(typeofnum);//numberconsole.log(typeofstr);//stringconsole.log(typeofund);//undefinedconsole.log(typeofnul);//objectconsole.log(typeofarr);//objectconsole.log(typeofobj);//objectconsole.log(typeoffun);//function

由结果可知typeof可以测试出number、string、boolean、undefined及function,而对于null及数组、对象,typeof均检测出为object,不能进一步判断它们的类型。

2.使用instanceof

console.log(boolinstanceofBoolean);// falseconsole.log(numinstanceofNumber);// falseconsole.log(strinstanceofString);// falseconsole.log(undinstanceofObject);// falseconsole.log(arrinstanceofArray);// trueconsole.log(nulinstanceofObject);// falseconsole.log(objinstanceofObject);// trueconsole.log(funinstanceofFunction);// truevarbool2 =newBoolean()console.log(bool2instanceofBoolean);// truevarnum2 =newNumber()console.log(num2instanceofNumber);// truevarstr2 =newString()console.log(str2instanceofString);//  truefunctionPerson(){}varper =newPerson()console.log(perinstanceofPerson);// truefunctionStudent(){}Student.prototype=newPerson()varhaoxl =newStudent()console.log(haoxlinstanceofStudent);// trueconsole.log(haoxlinstanceofPerson);// true

从结果中看出instanceof不能区别undefined和null,而且对于基本类型如果不是用new声明的则也测试不出来,对于是使用new声明的类型,它还可以检测出多层继承关系。

3.使用constructor

undefined和null没有contructor属性

console.log(bool.constructor===Boolean);// trueconsole.log(num.constructor===Number);// trueconsole.log(str.constructor===String);// trueconsole.log(arr.constructor===Array);// trueconsole.log(obj.constructor===Object);// trueconsole.log(fun.constructor===Function);// trueconsole.log(haoxl.constructor===Student);// falseconsole.log(haoxl.constructor===Person);// true

constructor不能判断undefined和null,并且使用它是不安全的,因为contructor的指向是可以改变的

4.使用Object.prototype.toString.call

console.log(Object.prototype.toString.call(bool));//[object Boolean]console.log(Object.prototype.toString.call(num));//[object Number]console.log(Object.prototype.toString.call(str));//[object String]console.log(Object.prototype.toString.call(und));//[object Undefined]console.log(Object.prototype.toString.call(nul));//[object Null]console.log(Object.prototype.toString.call(arr));//[object Array]console.log(Object.prototype.toString.call(obj));//[object Object]console.log(Object.prototype.toString.call(fun));//转载于[object Function]function Person(){}function Student(){}Student.prototype = new Person()var haoxl = new Student()console.log(Object.prototype.toString.call(haoxl));//[object Object]

原理(摘自高级程序设计3):在任何值上调用 Object 原生的 toString() 方法,都会返回一个 [object NativeConstructorName] 格式的字符串。每个类在内部都有一个 [[Class]] 属性,这个属性中就指定了上述字符串中的构造函数名。

但是它不能检测非原生构造函数的构造函数名。

5.使用jquery中的$.type

console.log($.type(bool));//booleanconsole.log($.type(num));//numberconsole.log($.type(str));//stringconsole.log($.type(und));//undefinedconsole.log($.type(nul));//nullconsole.log($.type(arr));//arrayconsole.log($.type(obj));//objectconsole.log($.type(fun));//functionfunctionPerson(){}functionStudent(){}Student.prototype=newPerson()varhaoxl =newStudent()console.log($.type(haoxl));//object

$.type()内部原理就是用的Object.prototype.toString.call()

转载于:https://segmentfault.com/a/1190000015264821

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容