数组类型判断


在 ECMAScript 5 中判断未知对象是否是数组,可以通过 Array.isArray() 来实现:

Array.isArray([1,2,3]);
Array.isArray({x:1, y:2});

在 ECMAScript 3 中由于没有 isArray 方法,需要这么实现:

var isArray = Function.isArray || function(o) {
  return typeof o === 'object' &&
    object.prototype.toString.call(o) === '[object Array]';  
};

点赞 取消点赞 收藏 取消收藏

<< 上一篇: 数组方法

>> 下一篇: 类数组对象