JavaScript判断变量是对象还是数组的方法
作者:bea
typeof都返回object 在JavaScript中所有数据类型严格意义上都是对象,但实际使用中我们还是有类型之分,如果要判断一个变量是数组还是对象使用typeof搞不定,因为它全都返回object 代码如下: var o = { 'name':'lee' }; var a = ['reg','blue']; document.write( ' o typeof is ' + typeof o); document.write( ' <br />
typeof都返回object
在JavaScript中所有数据类型严格意义上都是对象,但实际使用中我们还是有类型之分,如果要判断一个变量是数组还是对象使用typeof搞不定,因为它全都返回object
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
document.write( ' o typeof is ' + typeof o);
document.write( ' <br />');
document.write( ' a typeof is ' + typeof a);
执行:
代码如下:
o typeof is object
a typeof is object
因此,我们只能放弃这种方法,要判断是数组or对象有两种方法
第一,使用typeof加length属性
数组有length属性,object没有,而typeof数组与对象都返回object,所以我们可以这么判断
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(typeof o == 'object'){
if( typeof o.length == 'number' ){
return 'Array';
}else{
return 'Object';
}
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
第二,使用instanceof
使用instanceof可以判断一个变量是不是数组,如:
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
alert( a instanceof Array ); // true
alert( o instanceof Array ); // false
也可以判断是不是属于object
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
alert( a instanceof Object ); // true
alert( o instanceof Object ); // true
但数组也是属于object,所以以上两个都是true,因此我们要利用instanceof判断数据类型是对象还是数组时应该优先判断array,最后判断object
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(o instanceof Array){
return 'Array'
}else if( o instanceof Object ){
return 'Object';
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
如果你不优先判断Array,比如:
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(o instanceof Object){
return 'Object'
}else if( o instanceof Array ){
return 'Array';
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Object
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
那么数组也会被判断为object。
有用 | 无用
在JavaScript中所有数据类型严格意义上都是对象,但实际使用中我们还是有类型之分,如果要判断一个变量是数组还是对象使用typeof搞不定,因为它全都返回object
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
document.write( ' o typeof is ' + typeof o);
document.write( ' <br />');
document.write( ' a typeof is ' + typeof a);
执行:
代码如下:
o typeof is object
a typeof is object
因此,我们只能放弃这种方法,要判断是数组or对象有两种方法
第一,使用typeof加length属性
数组有length属性,object没有,而typeof数组与对象都返回object,所以我们可以这么判断
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(typeof o == 'object'){
if( typeof o.length == 'number' ){
return 'Array';
}else{
return 'Object';
}
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
第二,使用instanceof
使用instanceof可以判断一个变量是不是数组,如:
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
alert( a instanceof Array ); // true
alert( o instanceof Array ); // false
也可以判断是不是属于object
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
alert( a instanceof Object ); // true
alert( o instanceof Object ); // true
但数组也是属于object,所以以上两个都是true,因此我们要利用instanceof判断数据类型是对象还是数组时应该优先判断array,最后判断object
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(o instanceof Array){
return 'Array'
}else if( o instanceof Object ){
return 'Object';
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
如果你不优先判断Array,比如:
代码如下:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(o instanceof Object){
return 'Object'
}else if( o instanceof Array ){
return 'Array';
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Object
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
那么数组也会被判断为object。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 通过JS来动态的修改url,实现对url的增删查改
- 一个不错的仿携程自定义数据下拉选择select
- 原生javascript实现拖动元素示例代码
- 使用text方法获取Html元素文本信息示例
- textarea不能通过maxlength属性来限制字数的解决方法
- 超级好用的jQuery圆角插件 Corner速成
- Html5的placeholder属性(IE兼容)实现代码
- 通过js为元素添加多项样式,浏览器全兼容写法
- 原生js仿jq判断当前浏览器是否为ie,精确到ie6~8
- 原生js实现复制对象、扩展对象 类似jquery中的extend()方法
- ZeroClipboard插件实现多浏览器复制功能(支持firefox、chrome、ie6)
- 仿百度联盟对联广告实现代码
- jQuery针对各类元素操作基础教程
- jQuery事件用法实例汇总
- laytpl 精致巧妙的JavaScript模板引擎
- jQuery动画特效实例教程
- jQuery实用函数用法总结
- jQuery实现异步获取json数据的2种方式
- 原生javascript实现的分页插件pagenav