Confusion in destructuring in ES6

安稳与你 提交于 2019-12-10 11:13:35

问题


In the following JS (es6) code, what is going on with the variables inside of the curly braces with the colon?

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. What is this doing?


回答1:


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,

const { connectionType: hidingSpotConnection } = ...

takes the value from the connectionType key and stores it in a variable called hidingSpotConnection.



来源:https://stackoverflow.com/questions/42057106/confusion-in-destructuring-in-es6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!