【问题标题】:Confusion in destructuring in ES6ES6 中解构的困惑
【发布时间】:2017-02-05 20:47:54
【问题描述】:

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

const { foo: bar } = ...

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

【问题讨论】:

  • 是的,它正在解构。
  • @loganfsmyth,你能告诉我那里发生了什么吗?

标签: javascript ecmascript-6 destructuring


【解决方案1】:

最好把解构想成是声明对象的反面,所以在哪里

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

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

const { connectionType: hidingSpotConnection } = ...

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

【讨论】:

  • 如此优雅的解释立即揭开了主题的神秘面纱。谢谢。
猜你喜欢
  • 2022-06-16
  • 2017-01-17
  • 2018-02-20
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多