花括号内的变量起始

Variable initiation inside curly braces

本文关键字:变量      更新时间:2024-03-06

这段代码可以翻译成什么?我搞不清楚大括号内的变量是如何与= require('react-router')相关的。

var { create: createRouter, HistoryLocation, HashLocation } = 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

看起来是正在破坏赋值。它是Javascript ES6的一部分,在这里进行了描述。

析构函数赋值语法是一种JavaScript表达式,它可以使用反映数组和对象文字结构的语法从数组或对象中提取数据。

酷炫的新功能!我期待着使用它。