Variable initiation inside curly braces

commonjs, javascript, reactjs

Solution

This is a feature called destructuring assignment in ES6. This is what happens:

// Imagine this is the object you require
var reactRouter = {
  create: 'foo',
  HistoryLocation: 'bar',
  HashLocation: 'baz'
}

// Destructure
var {create: createRouter, HistoryLocation, HashLocation} = reactRouter

// Now the variables are in scope
console.log(createRouter, HistoryLocation, HashLocation)
//^ foo, bar, baz

Problem

What does this code translate to? I can't figure out how the variables inside the curly braces are related to `= require('react-router')`. ``` var { create: createRouter, HistoryLocation, HashLocation } = require('react-router') ``` It is from this repo

Original source

Related problems