3
  var type = (0, _reactIs.isMemo)(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type; 

(0, _reactIs.isMemo) really confuse me. What's the meaning of this?

ps: I know (0, _reactIs.isMemo) this expression's value is _reactIs.isMemo

webber
  • 69
  • 5
  • 5
    perhaps this is the result of some "transpiler" (e.g. babeljs) or "bundler" (e.g. webpack) process - so nobody has actually written this garbage, but some process has produced this valid, yet garbage, code – Jaromanda X Sep 15 '20 at 02:57
  • [Related](https://stackoverflow.com/q/53941926/1541563) – Patrick Roberts Sep 15 '20 at 03:06
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator – ksav Sep 15 '20 at 03:09
  • https://stackoverflow.com/questions/32275135/why-does-babel-rewrite-imported-function-call-to-0-fn – ksav Sep 15 '20 at 05:46

1 Answers1

10

The comma operator there ensures that what's inside the parentheses is evaluated as an expression without a calling context.

To take a shorter example, if the code was:

var type = obj.fn(someArg); 

then fn would be called with a calling context of obj. But the original untranspiled code, whatever it is, does not have such a calling context, so in order to be faithful to the original code, the calling context has to be removed, which can be done with the comma operator:

var type = (0, obj.fn)(someArg);

Another way of doing the same thing would be:

var fn = obj.fn;
var type = fn(someArg); 

(but that takes more characters, so minifiers prefer the comma operator version)

This is a silly-looking minification trick that's often seen with imported modules. Usually, you'd only be looking at the source code, which won't have this sillyness.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320