对象 属性 解构赋值?

Object property assignment with destructuring?

我想使用 ES6 destructuring 来分配对象的属性,但无法理解语法。

<= ES5:

var dst = {};  // already in existence, with its own props, methods, etc.
var src = { a: 'foo', b: 'bar', c: 'baz' };
dst.a = src.a;
dst.b = src.b;

>= ES6(我自己编造的无效语法):

let dst = {};
let src = { a: 'foo', b: 'bar', c: 'baz' };
dst[{a, b}] = src;

是否可以对对象使用解构赋值?正确的语法是什么?

编辑: 在我的用例中,dst 是一个在需要合并 src 属性的子集之前就已经存在的对象;它不是从 src.

专门为 'borrow' 创建的新对象

我认为你将不得不重复 dst:

({a: dst.a, b: dst.b} = src);

IMO 最干净的方法如下:

const dist = {a: 'foo', b: 'bar', c: 'baz'};

const {a, b} = dist;

const src = {a, b};

运行这个codepen

中的例子