beginner's: const definition in Redux confusin

2019-01-19 20:04发布

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;

I've just searched for ES6 const documentation, and it does not quite answer my question, how are these two lines identical?

1条回答
SAY GOODBYE
2楼-- · 2019-01-19 21:04

This is not related to const (which is just a way to define a constant), but instead to object destructuring.

So these are all identical:

var createStore = Redux.createStore;
const { createStore: createStore } = Redux;
const { createStore } = Redux;

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.

In addition, in ES6 defining objects like { name: name } can be shortened to { name }.

查看更多
登录 后发表回答