在變數內部進行解構

除了將物件解構為函式引數之外,你還可以在變數宣告中使用它們,如下所示:

const person = {
  name: 'John Doe',
  age: 45,
  location: 'Paris, France',
};

let { name, age, location } = person;

console.log('I am ' + name + ', aged ' + age + ' and living in ' + location + '.');
// -> "I am John Doe aged 45 and living in Paris, France."

如你所見,建立了三個新變數:nameagelocation,如果它們匹配鍵名,則從物件 person 中獲取它們的值。