扩展运算符

扩展运算符

  1. ... 扩展运算符能将 数组 转换为逗号分隔的 序列参数。

    const person = ['易烊千玺', '王俊凯', '王源'];
    
    let test = (...args) => {
        console.log(args)
    }
    function test() {
        console.log(arguments);
    }
    test(...person)// test('易烊千玺', '王俊凯', '王源')
    
  • 扩展运算符和剩余运算符很类似,区别是位置上不同
    • 扩展运算符是在实参里 或 合并数组
    • 剩余运算符是在形参里

应用场景

  1. 数组的合并

    const target1 = [];
    const target2 = [];
    // ES5
    const result = target1.concat(target2)
    // ES6
    const result = [...target1, ...targer2]
    
  2. 数组克隆(若子项没有引用则深拷贝,若有则浅拷贝)

    const arr = ['E','G','M'];
    const copyArr = [...arr];
    
  3. 将伪数组转为真正的数组

    const divs = document.querySelectorAll('div');
    const el = [...divs];
    
posted @ 2022-02-17 00:19  HuangBingQuan  阅读(333)  评论(0编辑  收藏  举报