2017-05-03 3 views
0

während eines Online-Kurses habe ich diese Syntax gefunden:Javascript Schlüsselwort const anders

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

Meine Frage ist: Was ist das bedeutet, dass der zweite Teil des Codes? Ist es ähnlich, einen Property-Namen aus der graphql-Bibliothek zu importieren? Ich überprüfte const Definition, einige andere Foren, aber ich finde nichts.

Dank

+0

[Destrukturierung Zuordnung] (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). Entspricht 'const = graphql.GraphQLObjectType;' – dfsq

Antwort

2

Dies ist ein Beispiel für ‚destructuring assignment‘, die man leicht extrahieren Teile eines Objekts oder einer Anordnung in eine Variable ermöglicht.

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]; 
Verwandte Themen