如何把类数组转为数组

首先说下什么是类数组:类数组对象拥有length属性,且可以在类数组对象上应用数组。举个类数组的的例子:getElementByTagName(‘div’)获取的dom序列为类数组,方法参数也为是类数组。数组概念就不再赘述了,下面是类数组转为数组的两种实现方法:

常用实现方法:
    Array.prototype.slice.call(类数组)

es6实现方法:
    Array.from(类数组)
示例:
(function(){
    console.log(Array.prototype.slice.call(arguments)); //[1,2,3]
    console.log(Array.from(arguments));//[1,2,3]
})(1,2,3)

猜你喜欢

转载自blog.csdn.net/yihanzhi/article/details/79558459