js中判断数据类型对的几种方法
2017-02-17 12:14
836 查看
判断js中的数据类型有一下几种方法:typeof、instanceof、 constructor、 prototype、 $.type()/jquery.type(),接下来主要比较一下这几种方法的异同。
1.typeof(有很多作用,如检查一个变量是否存在,是否有值;判断数据类型等作用)
var a=12;
var b='asb';
var c=new Date();
var d=function(){ alert(11);}
var e=function(){ this.name="lu";}
var f=[];
alert(typeof a); //number
alert(typeof b); //string
alert(typeof c); //object
alert(typeof d); //function
alert(typeof e); //function
alert(typeof f); //object
2.instantceof
//据对象的constructor判断: constructor
alert(f.constructor === Array);
//true
//注意: constructor 在类继承时会出错
3.prototype
alert(Object.prototype.toString.call(a)
=== '[object String]');//false
1.typeof(有很多作用,如检查一个变量是否存在,是否有值;判断数据类型等作用)
var a=12;
var b='asb';
var c=new Date();
var d=function(){ alert(11);}
var e=function(){ this.name="lu";}
var f=[];
alert(typeof a); //number
alert(typeof b); //string
alert(typeof c); //object
alert(typeof d); //function
alert(typeof e); //function
alert(typeof f); //object
2.instantceof
//据对象的constructor判断: constructor
alert(f.constructor === Array);
//true
//注意: constructor 在类继承时会出错
3.prototype
alert(Object.prototype.toString.call(a)
=== '[object String]');//false
相关文章推荐
- Js中数据类型判断的几种方法
- js 判断数据类型的几种方法
- 判断JS数据类型的几种方法
- js判断数据类型的几种方法及应用场景分析
- 判断js中的数据类型的几种方法
- 粗体判断js中的数据类型有一下几种方法
- 转:判断js中的数据类型的几种方法
- 判断js中的数据类型的几种方法
- js判断数据类型的几种方法
- 判断js中的数据类型的几种方法
- JS中判断数据类型的几种方法
- 判断js中的数据类型的几种方法
- js 判断数据类型的几种方法
- 判断js中的数据类型的几种方法
- js 判断数据类型的几种方法
- flex 判断数据类型的几种方法
- 转:Flex判断数据类型的几种方法
- 【Flex】Actionscript判断数据类型的几种方法比较
- js 判断各种数据类型的简单方法(推荐)
- JS小技巧:判断数据类型的一种方法