JS数据类型的判断

js 高博文
文章标签: js数据类型

JS数据类型的判断

       今天来分析一下JS中的数据类型的判断,JS中数据类型有两大类包括基本数据类型和引用数据类型。共有8种,null、undefined、string、Boolean、number、array、object、function。对数据类型的判断的方式有很多种,接下来就一 一分析:

(一)最常见的判断方法:typeof()

     typeof()返回的类型都是字符串形式,typeof()不能判断出数组是什么类型只是返回object代码如下:

                var num=123;
		var boo=true;
		var arr=["1","2"];
		var obj={name:"123",age:12};
		var str="qwer";
		var fun=function(){
			1+1;
		}
		console.log(typeof(num));//number
		console.log(typeof(null));//object
		console.log(typeof(boo));//boolean
		console.log(typeof(arr));//object
		console.log(typeof(obj));//object
		console.log(typeof(fun));//function
		console.log(typeof(str));//string
		console.log(typeof(undefined));//undefined

   typeof 可以对JS基础数据类型做出准确的判断,而对于引用类型返回的基本上都是object, 其实返回object也没有错,因为所有对象的原型链最终都指向了Object,Object是所有对象的`祖宗`。 但当我们需要知道某个对象的具体类型时,typeof 就显得有些力不从心了。

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

       instanceof 是用来判断已知的数据类型的对象。代码分析如下:

		var arr=["1","2"];
		var obj={name:"123",age:12};
		var fun=function(){
			1+1;
		}
		console.log(arr instanceof Array);//ture
                console.log([] instanceof Object;//true
		console.log(obj instanceof Object);//ture
		console.log(fun instanceof Function);//ture
		console.log(true instanceof Boolean);//false

从上面的代码可以发现 instanceof是用来判断已知实例化对象数据类型的判断,它能够检测出Array的实例,但是他同时也能检测出[]实例是object对象,其实instanceof是通过原型链的形式来查找再来匹配的。instanceof 只能用来判断两个对象是否属于原型链的关系, 而不能获取对象的具体类型。

(三)根据对象的constructor判断:constructor

     当定义一个构造函数的时候,构造函数就会添加一个prototype原型,然后再在原型上添加constructor属性,并指向构造函数的引用。当实例化构造函数的时候,实例化对象上会存在构造函数的原型。实例化丢向被构造函数标记为function类型。

(四)Object.prototype.toString

     tostring是Object原型对象上的一个方法,改方法返回其调用者的具体类型,返回类型包括:String、number、Boolean、undefined、null、function、array等所有对象的类型,代码如下:

var arr=["1","2"];
var obj={name:"123",age:12};
var fun=function(){
		1+1;
}
console.log(Object.prototype.toString.call(""));//objecft string
console.log(Object.prototype.toString.call(1));//objecft number
console.log(Object.prototype.toString.call(true));//objecft boolean
console.log(Object.prototype.toString.call(undefined));//objecft undefined
console.log(Object.prototype.toString.call(null));//objecft null
console.log(Object.prototype.toString.call(new Function()));//objecft Function
console.log(Object.prototype.toString.call(new Date()));//objecft Date
console.log(Object.prototype.toString.call([]));//objecft Array
console.log(Object.prototype.toString.call(arr));//objecft Array
console.log(Object.prototype.toString.call(obj));//objecft objecft
console.log(Object.prototype.toString.call(fun));//objecft Function

           从原型链的角度讲,所有对象的原型链最终都指向了Object, 按照JS变量查找规则,其他对象应该也可以直接访问到Object的toString方法,而事实上,大部分的对象都实现了自身的toString方法,这样就可能会导致Object的toString被终止查找,因此要用call来强制执行Object的toString方法。

(五)jQuery.type()

     jQuery.type()能更加万能的方法,它可以直接的返回数据的数据类型。代码如下:

var num=12;
var str="fds";
var arr=["1","2"];
var obj={name:"123",age:12};
var fun=function(){
		1+1;
}
console.log(jQuery.type(undefined));//undefined
console.log(jQuery.type(null));//null
console.log(jQuery.type());//undefined
console.log(jQuery.type(true));//boolean
console.log(jQuery.type(num));//number
console.log(jQuery.type(str));//string
console.log(jQuery.type(arr));//array
console.log(jQuery.type([]));//array
console.log(jQuery.type(obj));//objecft
console.log(jQuery.type(fun));//function
console.log(jQuery.type(new Date()));//date

(六)数组的判断:Array.isArray();

      这只是单纯的判断是不是数组,代码如下:

var arr=["1","2"];
var obj={name:"123",age:12};
console.log(Array.isArray(arr))//ture
console.log(Array.isArray([]))//ture
console.log(Array.isArray(obj))//fales

          通常情况下用typeof 判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,实在没辙就使用$.type()方法。

还能输出{{restrictNumber}}个字符  
  • {{reply.author}}

    {{CommonUtil.formateDate(reply.ac_CommentDate).shortTime}}
  • 回复了{{Comments.author}} :