1.JavaScript中有哪些类数组对象(伪数组)node
(1)arguments 函数的实参数组
function test() { return arguments; } let arg = test(1,4,7,4,3,6,4); console.log(arg);
(2)set 结构app
new Set([2,5,6,3,4,3,6,6]);
(3)nodeList函数
document.getElementsByTagName('div');
2.将类数组对象转为真实数组(以上文arguments对象为例, arg)prototype
(1)遍历类数组元素, 并push进一个新数组code
let newArr = []; for(let i=0; i<arg.length; i++) { newArr.push(arg[i]); }
(2)数组实例上的slice方法, 在不接受任何参数的状况下是复制一个数组, 再利用call(或apply)调用slice方法, 能够实现将类数组对象转为真实数组对象
Array.prototype.slice.call(arg); [].slice.call(arg);
(3)Array.from()方法, 能够将任何具备length属性的对象转为数组接口
Array.from(arg);
(4)扩展运算符..., 能够将全部布署了Iterator接口的对象转为数组ip
[...{length: 3}];
(5)jQuery的核心方法$.makeArray()get
$.makeArray(arg);