除了将对象分解为函数参数外,还可以在变量声明中使用它们,如下所示:
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."
如您所见,创建了三个新变量:name、age和location,如果它们与键名匹配,则从对象person获取它们的值。