Home > Web Front-end > JS Tutorial > How to Rename Target Variables During Object Destructuring in ES6/ES2015?

How to Rename Target Variables During Object Destructuring in ES6/ES2015?

Susan Sarandon
Release: 2024-10-18 12:59:30
Original
670 people have browsed it

How to Rename Target Variables During Object Destructuring in ES6/ES2015?

Destructuring with Alias in ES6/ES2015

Object destructuring is a powerful feature in ES6/ES2015 that allows us to extract values from objects and assign them to variables. However, sometimes we may want to rename the target variables during destructuring.

Renaming Target Variables

In your example, you tried to use the as syntax to rename the target variable like const {a, b as c} = test;, but this syntax is not valid in ES6/ES2015.

To rename target variables during object destructuring, you can simply assign new variable names, as shown in the MDN Example:

<code class="javascript">var o = { p: 42, q: true };

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

console.log(foo); // 42
console.log(bar); // true</code>
Copy after login

In this example, we destructure the o object and assign the value of the p property to the foo variable, and the value of the q property to the bar variable. The p and q properties of the o object are not modified.

The above is the detailed content of How to Rename Target Variables During Object Destructuring in ES6/ES2015?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template