初学者:Redux 中的 const 定义令人困惑 [英] beginner's: const definition in Redux confusing
问题描述
在这个 Redux 入门课程中https://egghead.io/lessons/javascript-redux-store-methods-getstate-dispatch-and-subscribe?series=getting-started-with-redux,主持人说下面两行是一样的
In this introductory course of Redux https://egghead.io/lessons/javascript-redux-store-methods-getstate-dispatch-and-subscribe?series=getting-started-with-redux, the presenter says that the following two lines are identical
const { createStore } = Redux;
var createStore = Redux.createStore;
我刚刚搜索了 ES6 const
文档,它并没有完全回答我的问题,这两行如何相同?
I've just searched for ES6 const
documentation, and it does not quite answer my question, how are these two lines identical?
推荐答案
这与const
(只是定义常量的一种方式)无关,而是与对象解构.
This is not related to const
(which is just a way to define a constant), but instead to object destructuring.
所以这些都是相同的:
var createStore = Redux.createStore;
const { createStore: createStore } = Redux;
const { createStore } = Redux;
在const { createStore: createStore } = Redux;
这一行中,第一个createStore
定义了Redux
要获取的属性.第二个 createStore
定义了在声明后可用的名称.
In the line const { createStore: createStore } = Redux;
, the first createStore
defines the property of Redux
to get. The second createStore
defines the name under which is available after the declaration.
另外,在 ES6 中定义像 { name: name }
这样的对象可以缩短为 { name }
.
In addition, in ES6 defining objects like { name: name }
can be shortened to { name }
.
这篇关于初学者:Redux 中的 const 定义令人困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!