ES6/ES2015 对象解构和改变目标变量

ES6/ES2015 object destructuring and changing target variable

如何在对象解构过程中重命名目标?

const b = 6;
const test = { a: 1, b: 2 };
const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015
// a === 1
// b === 6
// c === 2

您可以分配新的变量名称,如下所示MDN Example

var o = { p: 42, q: true };

// Assign new variable names
var { p: foo, q: bar } = o;

console.log(foo); // 42
console.log(bar); // true  


因此,在您的情况下,代码将如下所示

const b = 6;
const test = { a: 1, b: 2 };
let { a, b: c } = test;
console.log(a, b, c); // 1 6 2

Online Babel Demo