判断输入对象是否为数组

157 阅读1分钟

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;
}