问题
How can I rename the target during object destructing?
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
回答1:
You can assign new variable names, like shown in this 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
So, in your case, the code will be like this
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
来源:https://stackoverflow.com/questions/34904523/es6-es2015-object-destructuring-and-changing-target-variable