参见英文答案 > Javascript object bracket notation ({ Navigation } =) on left side of assign 4个
这段代码转化为什么?我无法弄清楚花括号内的变量如何与= 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