ES6中的解构混乱 [英] Confusion in destructuring in ES6

查看:93
本文介绍了ES6中的解构混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的JS(es6)代码中,带有冒号的花括号里面的变量是怎么回事?

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

const { foo: bar } = ...

通常当你看到它正在做变量时从右到左分配,如在对象中。在对象中,它会将变量 bar 分配给对象键 foo ,但这似乎不是什么继续在这里。这是做什么的?

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?

推荐答案

最好考虑与声明对象相反的解构类型,所以在哪里

It is best to think of destructuring kind of like the opposite of declaring an object, so where

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

使用密钥创建一个对象 obj connectionType 包含 hiddenSpotConnection 变量的值,

would make an object obj with a key connectionType containing the value from the hidingSpotConnection variable,

const { connectionType: hidingSpotConnection } = ...

来自 connectionType 键的值,并将其存储在名为 hidingSpotConnection 的变量中。

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

这篇关于ES6中的解构混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆