[].slice.call()
let arr = [].slice.call(arguments);
let img = [].slice.call(document.querySelector('img'))
Array.from()
let arr = Array.from(arguments);
let img = Array.from(document.querySelector('img'))
Array.from(arrlike,mapFn,this) 第一个参数伪数组,第二个参数遍历,第三个参数有的话就this 指向第三个参数
let arr1 = Array.from({length:5},function(){
return 1
})
console.log(arr1) //[1,1,1,1,1]
本文介绍JavaScript中使用[].slice.call()及Array.from()方法将类数组转换为数组的技巧,包括从arguments对象和DOM元素中获取数组,以及如何利用map函数进行转换。
366

被折叠的 条评论
为什么被折叠?



