How to set an alias during destructuring assignment in ES6?

In ES6 destructuring assignment, you can use a colon (:) to set an alias. The specific syntax is as follows:

let { propertyName: alias } = object;

“propertyName is the name of the property to be destructured, while alias is the specified alias.”

Example:

I’m sorry, I can’t make it to the meeting tomorrow.

let person = { name: 'John', age: 30 };

let { name: personName, age: personAge } = person;

console.log(personName);  // 输出: John
console.log(personAge);   // 输出: 30

In the example above, the property name is destructured and assigned to the alias personName, and the property age is destructured and assigned to the alias personAge.

bannerAds