gpt4 book ai didi

Javascript变量语句作为键值对

转载 作者:行者123 更新时间:2023-12-03 03:32:52 25 4
gpt4 key购买 nike

我正在使用 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/

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