2、ES6中的...

2019-10-28  本文已影响0人  小太阳可可

1、...为展开运算符
2、...为剩余运算符

    function sum2(...m){
        let total = 0;
        for(var i of m){
            total += i;
        }
        console.log(`total: ${total}`)  // total: 6
    }
    sum2(1,2,3)

    let sum3 = (...m)=>{
        let total = 0;
        for(var i of m){
            total += i;
        }
        console.log(`total: ${total}`)  // total: 6
    }
    sum3(1,2,3)

    let [x,y] = [4,8];
    console.log(...[4,8])   // 4 8

    let ary1 = [1,3]
    let ary2 = [4,8]
    console.log("concat:" + ary1.concat(ary2)); // [1,3,4,8]

    console.log([...ary1,...ary2])      // // [1,3,4,8]

    let [x,...y] = [1,2,3,4];
    console.log(y)  // [2,3,4]

初学,还不是很明白,不过就这些用法,先放这里吧!后期理解透了再回来;

上一篇 下一篇

猜你喜欢

热点阅读