Why does babel rewrite imported function call to (0, fn)(…)?

后端 未结 2 1509
无人及你
无人及你 2020-11-22 03:36

Given an input file like

import { a } from \'b\';

function x () {
  a()
}

babel will compile it to

2条回答
  •  花落未央
    2020-11-22 04:22

    (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

    0; // Ignore result
    var tmp = _b.a;
    tmp();
    

    (the , is the comma operator, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator).

提交回复
热议问题