Javascript 变量语句作为键值对
Javascript variable statement as key value pair
我在 javascript 中使用 Relay 进行编程,我发现了以下语句:
const { connectionType: UsersConnectionType } = connectionDefinitions( { nodeType: UserType });
{ connectionType: CustomerConnectionType }
到底是什么意思?以后如何引用该变量以及如果我还有两个这样的变量如何导出它,例如:
const { connectionType: CustomerConnectionType } = connectionDefinitions( { nodeType: CustomerType });
const { connectionType: ItemConnectionType } = connectionDefinitions( { nodeType: ItemType });
这是一种初始化 UsersConnectionType 的方法,请查看示例
function A(){
return {x : 5};
}
let {x : y} = A();
console.log(y);
如果你知道函数的 return 值是什么,你可以这样初始化它们。
{x : y}
此处 y 从函数 A()
中分配了 x return 的值
我想我刚找到它 - 它是 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 中使用 Relay 进行编程,我发现了以下语句:
const { connectionType: UsersConnectionType } = connectionDefinitions( { nodeType: UserType });
{ connectionType: CustomerConnectionType }
到底是什么意思?以后如何引用该变量以及如果我还有两个这样的变量如何导出它,例如:
const { connectionType: CustomerConnectionType } = connectionDefinitions( { nodeType: CustomerType });
const { connectionType: ItemConnectionType } = connectionDefinitions( { nodeType: ItemType });
这是一种初始化 UsersConnectionType 的方法,请查看示例
function A(){
return {x : 5};
}
let {x : y} = A();
console.log(y);
如果你知道函数的 return 值是什么,你可以这样初始化它们。
{x : y}
此处 y 从函数 A()
中分配了 x return 的值我想我刚找到它 - 它是 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;
只是更多的闪闪发光和混乱。 :)