Javascript object bracket notation ({ Navigation } =) on left side of assign

JavascriptWebpackEcmascript 6

Javascript Problem Overview


I haven't seen this syntax before and am wondering what it's all about.

var { Navigation } = require('react-router');

The brackets on the left are throwing a syntax error:

> unexpected token {

I'm not sure what part of the webpack config is transforming or what the purpose of the syntax is. Is it a Harmony thing? Can someone enlighten me?

Javascript Solutions


Solution 1 - Javascript

It's called destructuring assignment and it's part of the ES2015 standard.

> The destructuring assignment syntax is a JavaScript expression that > makes it possible to extract data from arrays or objects using a > syntax that mirrors the construction of array and object literals. > > Source: Destructuring assignment reference on MDN

Object destructuring

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

 console.log(p); // 42
 console.log(q); // true 

 // Assign new variable names
 var {p: foo, q: bar} = o;

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

Array destructuring

var foo = ["one", "two", "three"];

// without destructuring
var one   = foo[0];
var two   = foo[1];
var three = foo[2];

// with destructuring
var [one, two, three] = foo;

Solution 2 - Javascript

This is destructuring assignment. It's a new feature of ECMAScript 2015.

var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

Is the equivalent to:

var AppRegistry = React.AppRegistry;
var StyleSheet = React.StyleSheet;
var Text = React.Text;
var View = React.View;

Solution 3 - Javascript

var { Navigation } = require('react-router');

... uses destructuring to achieve the same thing as ...

var Navigation = require('react-router').Navigation;

... but is far more readable.

Solution 4 - Javascript

It's a new feature in ES6 to destructure objects.

As we all know that there is an assignment operation taking place here, which means right side value is getting assigned to left side variable.

var { Navigation } = require('react-router');

In this case require('react-router') method returns an object with key-value pair, something like:

{ Navigation: function a(){}, 
Example1: function b(){},
 Example2: function c(){}
}

And if we would like to take one key in that returned object say Navigation to a variable we can use Object destructuring for that.

This will only be possible only if we have the key in hand.

So after the assignment statement, local variable Navigation will contain function a(){}

Another example looks like this.

var { p, q } = { p: 1, q:2, r:3, s:4 };
console.log(p) //1;
console.log(q) //2;

Solution 5 - Javascript

instead of

const salary = personnel.salary
const sex = personnel.sex
const age = personnel.age

simply

const {salary, age, sex} = personnel

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncaptainillView Question on Stackoverflow
Solution 1 - JavascriptMatt BallView Answer on Stackoverflow
Solution 2 - JavascriptRudolf ManusachiView Answer on Stackoverflow
Solution 3 - JavascriptCliff HallView Answer on Stackoverflow
Solution 4 - JavascriptRajendra kumar VankadariView Answer on Stackoverflow
Solution 5 - JavascriptAkin ZemanView Answer on Stackoverflow