Given an input file like
JavaScript
x
6
1
import { a } from 'b';
2
3
function x () {
4
a()
5
}
6
babel will compile it to
JavaScript
1
8
1
'use strict';
2
3
var _b = require('b');
4
5
function x() {
6
(0, _b.a)();
7
}
8
but when compiled in loose mode the function call is output as _b.a();
I’ve done some research into where the comma operator is added in the hope there was a comment explaining it. The code responsible for adding it is here.
Advertisement
Answer
(0, _b.a)()
ensures that the function _b.a
is called with this
set to the global object (or if strict mode is enabled, to undefined
). If you were to call _b.a()
directly, then _b.a
is called with this
set to _b
.
(0, _b.a)();
is equivalent to
JavaScript
1
4
1
0; // Ignore result
2
var tmp = _b.a;
3
tmp();
4
(the ,
is the comma operator, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator).