花括号内的变量初始化

Variable initiation inside curly braces

这段代码翻译成什么?我无法弄清楚花括号内的变量与 = require('react-router') 有何关系。

var { create: createRouter, HistoryLocation, HashLocation } = require('react-router')

来自this repo

这是 ES6 中称为 destructuring assignment 的功能。这是发生了什么:

// 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

看起来是解构赋值。它是 Javascript ES6 and is described here.

的一部分

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.

很酷的新功能!我很期待使用它。