判断 js 的 Array 和 Object

本文介绍了JavaScript中Array与Object类型的检测方法,包括使用typeof、toString及ES2005新增的Array.isArray方法的区别与应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 https://my.oschina.net/ohcoding/blog/470952?p=1

    var a = ['hello','world'];
    console.log(typeof a); // object
    console.log(a.toString());  // hello,word 字符串
    console.log(Object.prototype.toString.call(a)); //[object Array]

    var b = {'hello':'world'};
    console.log(typeof b); // object
    console.log(b.toString()); // [object Object]
    console.log(Object.prototype.toString.call(b));// [object Object]

1. Array 和 Object 的typeof 结果都是 object

2. toString() Array的结果是真的字符串,而Object的结果是固定的[object Object]

3. 通过Object.prototype.toString调用,Array的结果返回固定[object Array],Object的结果依然是[object Object]

 

ES2005 新方法 

    console.log( Array.isArray(a) ); // true
    console.log( Array.isArray(b) ); // false

 

转载于:https://www.cnblogs.com/mitang/p/5756559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值