Javascript const 关键字的使用方式不同

标签 javascript ecmascript-6 graphql

在在线类(class)中我发现了这个语法:

const graphql = require('graphql');
const{
     GraphQLObjectType
} = graphql;
...

我的问题是:代码的第二部分是什么意思?就像从 graphql 库导入一些属性名称一样? 我检查了 const 定义和其他一些论坛,但没有找到任何内容。

谢谢

最佳答案

这是“destructuring assignment”的示例',它允许您轻松地将对象或数组的一部分提取到变量中。

const { GraphQLObjectType } = graphql;

// is the same as

const GraphQLObjectType = graphql.GraphQLObjectType;

let obj = { a: 0, b: 1 };
let { a, b } = obj;

// is the same as

let obj = { a: 0, b: 1 };
let a = obj.a;
let b = obj.b;

var arr = [0, 1, 2];
var [ a, b ] = arr;

// is the same as

var arr = [0, 1, 2];
var a = arr[0];
arv b = arr[1];

关于Javascript const 关键字的使用方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43760027/

相关文章:

graphql - 使用 graphiql 检查远程 graphql 端点

javascript - 我的输入 : onChange handler is not working on mobile

javascript - 如何在WordPress中使用PHP自动将选项中的值发布到输入值

JavaScript 获取。如何处理结果

javascript - React 图像在本地加载,但不在 AWS Amplify 上

javascript - 使用 .catch() 处理未处理的 Promise 拒绝

graphql - GraphQL 中的社交身份验证

php - 根据选项更改价格

javascript - 如何正确等待模板在 Angular 2 中更新

reactjs - AWS AppSync + React-Apollo Query/useQuery 引发异常 this.currentObservable.query.getCurrentResult 不是函数