函数特点
- arrayLike - 伪数组对象【字符串,NodeList,arguments】或可迭代对象【Set,Map】
- mapFn - 新数组中每个元素都会执行该回调函数
- thisArg - 执行回调函数mapFn时this对象
- 返回值 - 新数组实例
- 若参数是一个真正的数组,则会返回一个一模一样的新数组【浅拷贝】
函数用法
Array.from(arrayLike, mapFn, thisArg)
使用场景
const ary = [{ name: 'abc' }]
const copyAry = Array.from(ary)
copyAry[0].name = 'abcd'
console.log(ary)
Array.from('abc');
const m = new Map().set(1, 2).set(3, 4);
Array.from(m);
const s = new Set().add(1).add(2).add(3).add(4);
Array.from(s);
const aryList = {};
cosnt nodes = document.querySelectorAll('li');
Array.from(nodes);
function fn() {
return Array.from(arguments);
}
fn(1, 2, 3);