0

I was playing around on the Babel repl, when I noticed, for:

import {foo} from 'bar';
foo();

Babel generates:

'use strict';

var _bar = require('bar');
(0, _bar.foo)();

whereas for:

function foo() {}
foo();

Babel generates the expected:

"use strict";

function foo() {}
foo();

My question is with regards to the last line of the first Babel output, is there a reason for doing (0, _bar.foo)(); instead of _bar.foo()? Does it handle some sort of corner case if _bar.foo is undefined?

I found these resources. From MDN comma operator docs:

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

and from MDN grouping operator docs:

The grouping operator ( ) controls the precedence of evaluation in expressions.

From my understanding, from those two statements, (0, _bar.foo) should evaluate to _bar.foo?

hargasinski
  • 841
  • 5
  • 14
  • 1
    Yes, your understanding is correct. The difference to a plain `_bar.foo()` is the value of `this` inside `foo`. – Bergi Mar 23 '17 at 03:14
  • Yes, the linked question helps a lot, thanks! I don't know why it didn't come up when I googled this. – hargasinski Mar 23 '17 at 03:16

0 Answers0