日期:2014-05-16  浏览次数:20369 次

JavaScript判断是否为数组

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

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