JavaScript判断变量是对象还是数组的方法

作者:junjie 时间:2024-06-24 06:29:37 

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,判断变量,对象,数组
0
投稿

猜你喜欢

  • 新年快乐! python实现绚烂的烟花绽放效果

    2022-01-15 13:18:00
  • Python实现以时间换空间的缓存替换算法

    2021-03-31 13:45:16
  • 解决Python下json.loads()中文字符出错的问题

    2022-06-17 21:16:52
  • Pandas Shift函数的基础入门学习笔记

    2023-02-16 20:46:12
  • Python requests接口测试实现代码

    2023-09-10 18:09:21
  • Python装饰器的练习题

    2023-12-07 13:11:48
  • Python3之手动创建迭代器的实例代码

    2021-10-15 21:00:30
  • Python学习小技巧之列表项的排序

    2022-03-31 12:06:12
  • scrapy爬虫:scrapy.FormRequest中formdata参数详解

    2022-10-02 10:28:38
  • linux/mac安装mysql忘记密码的解决办法

    2024-01-23 23:40:06
  • Python3 XML 获取雅虎天气的实现方法

    2023-02-13 16:35:38
  • Django实现WebSocket在线聊天室功能(channels库)

    2023-04-01 00:41:55
  • Adobe AIR beta 2震撼发布!

    2007-10-07 11:57:00
  • 解决MySql版本问题sql_mode=only_full_group_by

    2024-01-14 00:23:17
  • Python 标准库 fileinput与文件迭代器

    2023-10-31 22:36:50
  • mysql5.x升级到mysql5.7后导入之前数据库date出错的快速解决方法

    2024-01-22 10:09:17
  • Pycharm学习教程(7)虚拟机VM的配置教程

    2022-10-12 16:49:33
  • 如何使用Python修改matplotlib.pyplot.colorbar的位置以对齐主图

    2021-09-28 18:01:30
  • python多线程编程方式分析示例详解

    2023-05-13 04:56:01
  • Dreamweaver表格布局经验谈

    2007-02-03 11:39:00
  • asp之家 网络编程 m.aspxhome.com