1. 定义:拥有length属性的对象
var o1 = { 2: 2, 1: 1, length: 4, a: 3 }
var o2 = { 2: 2, 1: 1, a: 3 }
Array.prototype.slice.call(o1)
Array.prototype.slice.call(o2)
Array.prototype.join.call(o1, '')
Array.prototype.join.call(o2, '')
2. 特点
- (对象)不具备数组的方法
- 索引类属性:非负整数(当做字符串解析)
Object.keys(o2)[0]
o2[1]
o2['1']
3. 常见例子
function f() {
return arguments
}
var arg = f(1, 2)
Array.isArray(arg)
arg
arg.length
Array.prototype.slice.call(arg)
var eles = document.getElementsByTagName('p')
eles
Array.isArray(eles)
eles.length
Array.prototype.slice.call(eles)
4. 类数组转化为数组
- Array.prototype.slice.call
- Array.from
var o1 = { 2: 2, 1: 1, length: 4 }
Array.isArray(Array.prototype.slice.call(o1))
Array.from(o1)
Array.isArray(Array.from(o1))