第一种:使用Array.from( )
作用:从类数组对象或者可迭代对象中创建一个新的数组实例
function test(a, b, c) { console.log(arguments); // 1.Array.from(); console.log(Array.from(arguments)); } test(1, 2, 3, 4, 5)
第二种:使用Array.prototype.slice()+ call()
数组名.slice(start, end):
- 作用1:方法可从已有的数组中返回选定的元素。
- 作用2:可提取字符串的某个部分,并以新的字符串返回被提取的部分。
- 返回值:返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。
b.call(a,事件参数)
- 将b中的this指向a
function test(a, b, c) { console.log(arguments); // 2. Array.prototype.slice.call(arguments, 0) console.log(Array.prototype.slice.call(arguments)); //等价于arguments.slice() 但为什么不可以直接这样写? // slice()是数据API 但是arguments是类数组 所以不能使用,可以通过call 改变slice中this的指向 才可以用 } test(1, 2, 3, 4, 5)
第三种:使用拓展运算符
使用方法:var arr=[...argument]
function test(a, b, c) { console.log(arguments); // 3...拓展运算符 let arr = [...arguments]; console.log(arr); } test(1, 2, 3, 4, 5)