javascript – 花括号内的变量启动

参见英文答案 > 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
上一篇:javascript – Process.chdir()对require没有影响


下一篇:任何IDE / IDE插件是否都支持JavaScript CommonJS模块?