es6 ...扩展运算符

展开运算符(用三个连续的点 (...) 表示)是 ES6 中的新概念,使你能够将字面量对象展开为多个元素

const order = [20.17, 18.67, 1.50, "cheese", "eggs", "milk", "bread"];

const [total, subtotal, tax, ...items] = order;

console.log(total);

console.log(subtotal);

console.log( tax);

console.log(items);

> 20.17

> 18.67

> 1.5

> Array ["cheese", "eggs", "milk", "bread"]


可变参数函数

在ES6中使用剩余参数运算符则更为简洁,可读性提高:

function sum(...nums) {

  let total = 0; 

  for(const num of nums) {

    total += num;

  }

  return total;

}


配合assign添加多个属性

const obj = {

    sex : 1,

    addName(){

        this.name = 'cxh'

    },

    setAge(n){

        this.age = n;

    }

}

const obj1 = Object.assign({},obj)

obj1

{sex: 1, addName: ƒ, setAge: ƒ}

    addName:ƒ addName()

    setAge:ƒ setAge(n)

    sex:1

你可能感兴趣的:(es6 ...扩展运算符)