ES6对象和数组解构

解构可以避免在对象赋值时再生成多余的中间变量:

function foo() {
  return [1,2,3];
}
let arr = foo(); // [1,2,3]

let [a, b, c] = foo();
console.log(a, b, c); // 1 2 3

function bar() {
  return {
    x: 4,
    y: 5,
    z: 6
  };
}
let {x: x, y: y, z: z} = bar();
console.log(x, y, z); // 4 5 6

 

posted @ 2017-10-25 11:11  佳琪欧尼  阅读(463)  评论(0)    收藏  举报