什么是类数组对象?
拥有length属性和若干索引属性的对象, 类数组只有索引值和长度,没有数组的各种方法 这个就叫做类数组对象
一个简单的类数组对象:
1 |
var arrayLike = {0: 'name', 1: 'age', 2: 'sex', length: 3 } |
如何把一个类数组对象给转成数组呢?
1 2 3 4 5 6 7 8 |
// 1. slice Array.prototype.slice.call(arrayLike); // ["name", "age", "sex"] // 2. splice Array.prototype.splice.call(arrayLike, 0); // ["name", "age", "sex"] // 3. ES6 Array.from Array.from(arrayLike); // ["name", "age", "sex"] // 4. apply Array.prototype.concat.apply([], arrayLike) |