此代码转化为什么?我无法弄清楚花括号内的变量是如何与= require('react-router')
相关的。
var { create: createRouter, HistoryLocation, HashLocation } = require('react-router')
答案 0 :(得分:6)
这是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
答案 1 :(得分:0)
看起来它是解构分配。它是Javascript ES6 and is described here.
的一部分解构赋值语法是一个JavaScript表达式,它可以使用反映数组和对象文字构造的语法从数组或对象中提取数据。
酷炫新功能!我很期待使用它。