gpt4 book ai didi

javascript - ES6 解构中的困惑

转载 作者:行者123 更新时间:2023-11-29 16:06:04 24 4
gpt4 key购买 nike

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

const { foo: bar } = ...

通常当您看到它时,它正在从右到左进行变量赋值,就像在对象中一样。在对象中,它会将变量 bar 分配给对象键 foo,但这似乎不是这里发生的事情。这是在做什么?

最佳答案

最好将解构看作是声明对象的反面,所以在哪里

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

将创建一个对象 obj,其键 connectionType 包含来自 hidingSpotConnection 变量的值,

const { connectionType: hidingSpotConnection } = ...

connectionType 键中获取值并将其存储在名为 hidingSpotConnection 的变量中。

关于javascript - ES6 解构中的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42057106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com