Javascript 花括号内的变量初始化
这段代码翻译成什么?我无法理解大括号内的变量如何与Javascript 花括号内的变量初始化,javascript,reactjs,commonjs,Javascript,Reactjs,Commonjs,这段代码翻译成什么?我无法理解大括号内的变量如何与=require('react-router')相关 它来自这是ES6中的一个功能。情况就是这样: // Imagine this is the object you require var reactRouter = { create: 'foo', HistoryLocation: 'bar', HashLocation: 'baz' } // Destructure var {create: createRouter, His
=require('react-router')
相关
它来自这是ES6中的一个功能。情况就是这样:
// 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
看起来像是在分解作业。它是 destructuring assignment语法是一个JavaScript表达式,它可以使用反映数组和对象文本构造的语法从数组或对象中提取数据
酷的新功能!我很期待使用它。解构作业。很好的解释。看起来很有趣!谢谢
// 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