欢迎来到码农权的博客 MaNongGeGe|

扩展运算符

扩展运算符

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

本文作者:HuangBingQuan

本文链接:https://www.cnblogs.com/bingquan1/p/15902927.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   HuangBingQuan  阅读(338)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 不将就 码农权
  2. 2 Bleeding Love 码农权
  3. 3 想你念你 码农权
Bleeding Love - 码农权
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Not available