简体   繁体   English

ES6中的解构混乱

[英]Confusion in destructuring in ES6

In the following JS (es6) code, what is going on with the variables inside of the curly braces with the colon? 在下面的JS(es6)代码中,带有冒号的花括号内的变量怎么回事?

const { foo: bar } = ...

Normally when you see this it is doing variable assignment from right to left, as in Objects. 通常,如您在对象中看到的那样,它是从右到左进行变量分配。 In objects it would assign the variable bar to the object key foo , but that doesn't seem to be what is going on here. 在对象中,它将变量bar分配给对象键foo ,但这似乎不是这里发生的事情。 What is this doing? 这是在做什么

It is best to think of destructuring kind of like the opposite of declaring an object, so where 最好将销毁视为与声明对象相反的方式,因此在哪里

const hidingSpotConnection = ...
const obj = { connectionType: hidingSpotConnection };

would make an object obj with a key connectionType containing the value from the hidingSpotConnection variable, 将使对象obj的键connectionType包含hidingSpotConnection变量中的值,

const { connectionType: hidingSpotConnection } = ...

takes the value from the connectionType key and stores it in a variable called hidingSpotConnection . connectionType键中获取值,并将其存储在一个名为hidingSpotConnection的变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM