<分区>
这段代码翻译成什么?我无法弄清楚花括号内的变量如何与 = require('react-router')
相关。
var { create: createRouter, HistoryLocation, HashLocation } = require('react-router')
<分区>
这段代码翻译成什么?我无法弄清楚花括号内的变量如何与 = require('react-router')
相关。
var { create: createRouter, HistoryLocation, HashLocation } = require('react-router')
最佳答案
这是一个名为 destructuring assignment 的功能在 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
关于javascript - 花括号内的变量初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28665764/
相关文章:
javascript - jQuery 检测 PHP 页面加载时输入的变化
javascript - 如何使用 Angular 指令同时操作多个 dom?
javascript - 顶部带有箭头的 React 水平滚动卡片
javascript - Ionic Framework (AngularJS) 和数据库 SQLite 3
javascript - React.js教程: comments don't append in the list
typescript - Typescript 和 Browserify 可以一起使用吗?