beginner's: const definition in Redux confusin

2019-01-19 20:13发布

问题:

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:

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 }.