es6中三个点的使用(...)----rest参数、解构赋值

1.在函数中作为参数使用,表示该参数个数不定

let sum = (...num) =>{
    let total = 0;
    for(var i of num){
        total += i;
    } 
    console.log(`total:${total}`)
}

sum(1,2,3,4,5,6)  //计算参数的总和

2.合并数组

let arr1 = [1,2,3];
let arr2 = [4,5,6];
console.log([...arr1,...arr2]) //[1, 2, 3, 4, 5, 6]

3.解构赋值

var [x,y] = [1,2] //x=1,y=2
var [x,y] = [1,2,3,4,5] //x=1,y=2
var [x,...y] = [1,2,3,4,5] //x=1,y=[2,3,4,5]
var [x,y,z] = 'es6' //x=e,y=s,z=6
var [x,y,z] = 'javascript' //x=j,y=a,z=v
var a = [...'es6'] // a=['e','s','6']

 

你可能感兴趣的:(es6)