• ES6扩展运算符


    博客链接:https://blog.csdn.net/astonishqft/article/details/82899965

    用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中。

    const [first, ...rest] = [1, 2, 3, 4, 5]; 
    first // 1
    rest // [2, 3, 4, 5]
    • 扩展运算符还可以将字符串转为真正的数组
    [...'hello']// [ "h", "e", "l", "l", "o" ]

    作用:

    1 展开一个数组

        let arr1 = [1,3,5,7,9]
      let arr2 = [2,4,6,8,10]
        console.log(arr1)
        console.log(...arr1)//展开一个数组

    2 连接两个数组 

        let  arr3 = [...arr1,...arr2]//链接数组
    

      

    3 传入参数

        function sum (...numbers) {
            return numbers.reduce((prevalue,currentvalue)=>{
                return prevalue+currentvalue
            })
        }

    4 对象不能展开,但是可以通过字面量进行赋值

        let person = {name:'tom',age:18}
        let person2 = person
        let person3 = {...person}//...person是,{}字符量的形式 赋值!!!
        //console.log(...person);//展开运算符不能展开对象。

    5 添加

        let  person4 ={...person,name:'cc',address:'地球'}
        console.log(person4);

    6 比较常见的应用是可以将某些数据结构转为数组,比如:

    // arguments对象
    function foo() {
      const args = [...arguments];
    }
  • 相关阅读:
    3.node.js AssertionError: false == true错误解决
    9.获取当前时区时间和utc时间的工具方法
    2.Express封装mysq方法
    1.Express初识
    poj 3617
    前缀和
    pop 反序列化
    Reverse前两个题
    前两个Web题
    Misc
  • 原文地址:https://www.cnblogs.com/hacker-caomei/p/14335256.html
Copyright © 2020-2023  润新知