类数组
类数组:对象拥有length属性,其它属性(索引)为非负整数的字符串
类数组判断
变量类型:对象
length: 不能为无穷,不能超过JavaScript数组极限长度的自然数
《javascript权威指南》上给出了代码用来判断一个对象是否属于“类数组”。如下:
// Determine if o is an array-like object.// Strings and functions have numeric length properties, but are // excluded by the typeof test. In client-side JavaScript, DOM text// nodes have a numeric length property, and may need to be excluded // with an additional o.nodeType != 3 test.function isArrayLike(o) {
if (o && // o is not null, undefined, etc.
typeof o === 'object' && // o is an object
isFinite(o.length) && // o.length is a finite number
o.length >= 0 && // o.length is non-negative
o.length===Math.floor(o.length) && // o.length is an integer
o.length < 4294967296) // o.length < 2^32
return true; // Then o is array-like
else
return false; // Otherwise it is not
}
与数组的区别:
不能直接调用数组的方法,可以通过借用数组的方法,例如
Array.prototype.map.call类数组对象转化为数组
1.Array.prototype.slice.call(arguments) 2._.toArray()

浙公网安备 33010602011771号