2

I am a beginner in ES6 and I was checking out Babel 6. I imported Redux using the statement

import {createStore} from 'redux';

and later used it like this

const store = createStore(myReducer);

When I ran Babel with ES2015 plugin, it transformed the code to

var store = (0, _redux.createStore)(myReducer);

What does this (0, _redux.createStore) mean ?

ajaybc
  • 4,049
  • 7
  • 44
  • 57
  • In that form, `(0, ..)` is a sequence expression (surrounded by parenthesis). It evaluates to the last expression - consider: `1 === (3,2,1)`. The last expression evaluates to a function which is invoked, but I'm not sure why the form is used over a more direct-looking `(_redux.createStore)(..)`. Remember that it is function *objects/values* - as resulting from any expression - that are invoked. – user2864740 Mar 22 '16 at 05:04
  • IIRC, I have a very vague recollection of changing the `this` binding in strict/non-strict modes.. although https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator says nothing on the topic. – user2864740 Mar 22 '16 at 05:09
  • Whoop! Found it :D – user2864740 Mar 22 '16 at 05:18

1 Answers1

0

What does this (0, _redux.createStore) mean ?

Babel transpiled the imported value of redux into a temporary variable _redux and then uses that for any references that chain off of _redux e.g. createStore. So we have _redux.createStore.

This helps preserve context (a video on this is helpful) on the called function.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • An output of `_redux.createStore(myReducer)` would still make the first part of the answer true .. and yet it says nothing about the sequence grammar expression / comma operator used. – user2864740 Mar 22 '16 at 05:10