Comma operator with IIFEs

96 Views Asked by At

Inside of the react-redux library, in the file defining useSelector, I have found multiple instances of comma operators used with IIFEs in the follow manner:

    var _useReducer = (0, _react.useReducer)(function (s) {
    return s + 1;
  }, 0),
      forceRender = _useReducer[1];
      
  var subscription = (0, _react.useMemo)(function () {
    return (0, _Subscription.createSubscription)(store, contextSub);
  }, [store, contextSub]);
  var latestSubscriptionCallbackError = (0, _react.useRef)();
  var latestSelector = (0, _react.useRef)();
  var latestStoreState = (0, _react.useRef)();
  var latestSelectedState = (0, _react.useRef)();

I can't understand or find any reference as to why they are using 0 inside of the IIFEs.

In the context of the comma operator, each value is evaluated and only the last expression is assigned to the variable.

In my eyes and to my understanding, the 0s are meaningless. Can someone explain them?

1

There are 1 best solutions below

0
On BEST ANSWER

The reason it does this is that calling a function from a comma operator puts the called function in the global context. (0, function)() the zero is just a convention.

If it was not called using this method this (the context) would be the local scope. You often find this behavior in transpiled code such as babel or TypeScript.