Javascript变量语句作为键值对

标签 javascript relay

我正在使用 Relay 在 javascript 中进行编程,我发现了以下语句:

const { connectionType: UsersConnectionType } = connectionDefinitions( { nodeType: UserType });

{ connectionType: CustomerConnectionType } 到底是什么意思?稍后如何引用该变量以及如果我还有两个以上变量如何导出它,例如:

const { connectionType: CustomerConnectionType } = connectionDefinitions( { nodeType: CustomerType });
const { connectionType: ItemConnectionType } = connectionDefinitions( { nodeType: ItemType });

最佳答案

我想我刚刚找到了它 - 这是一个 Destructuring assignment 。还有其他形式的数组等。这是 ECMAScript 6 的东西。

A property can be unpacked from an object and assigned to a variable with a different name than the object property.

var o = {p: 42, q: true};
var {p: foo, q: bar} = o;

console.log(foo); // 42 
console.log(bar); // true

坦白说,我认为语法很困惑 - 我会使用 var { foo: p, bar: q} 代替(或者完全不同的语法,所以它看起来不会令人困惑地类似于对象初始化),但我想他们有他们的理由。

在你的情况下,该行

const { connectionType: UsersConnectionType } = connectionDefinitions({nodeType:UserType});

实际上相当于

const UsersConnectionType = connectionDefinitions({nodeType:UserType}).connectionType;

只是有更多的金光闪闪和困惑。 :)

关于Javascript变量语句作为键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007712/

相关文章:

如果用户更改文本区域中的文本,JavaScript 函数将失败

javascript - Laravel Ajax - 未捕获的 ReferenceError : KWS1389776 is not defined at HTMLButtonElement. onclick

javascript - 无法获取在react-router-relay 上使用的userID 路由参数

javascript - BabelPluginRelay : Expected plugin context to include "types", 但得到 :[object Object] when upgrading from 1. 1.0 到 1.4.1。为什么?

node.js - Apollo Stack 是否像 Relay 的 Node 接口(interface)一样支持全局对象识别?

javascript - 使用 CSS 的表格列样式

javascript - 如何在另一个函数中使用在函数中声明的变量?

javascript - 页脚定位 : footer not appearing at the bottom of the page

graphql - 中继编译器不生成 .graphql 文件

reactjs - 独立的 graphql/relay 后端和前端