Ruby's Louvre

每天学习一点点算法

导航

判断是否为数组

判断一个对象是否为数组比较麻烦,以下是我收集的各种版本

Douglas Crockford的版本

 
  var isArray = function(a){
    return a &&
      typeof a === 'object' &&
      typeof a.length === 'number' &&
      typeof a.splice === 'function' &&
      !(a.propertyIsEnumerable('length'));
  }

Ext与JQuery的版本

 
  var isArray = function(v){
    return Object.prototype.toString.apply(v) === '[object Array]';
  }

Prototype的版本

 
  var isArray = function(object) {
    return object != null && typeof object === "object" &&
      'splice' in object && 'join' in object;
  }

posted on 2009-09-15 22:50  司徒正美  阅读(1914)  评论(7编辑  收藏  举报