其他分享
首页 > 其他分享> > 扩展运算符

扩展运算符

作者:互联网

扩展运算符

  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];
    

标签:...,const,扩展,运算符,数组,test,target1
来源: https://www.cnblogs.com/bingquan1/p/15902927.html