问题描述
给定一个输入文件,例如
Given an input file like
import { a } from 'b';
function x () {
a()
}
babel将编译到
'use strict';
var _b = require('b');
function x() {
(0, _b.a)();
}
但是当编辑松动模式时,函数调用输出为 _b.a();
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.
推荐答案
(0,_b.a)()
确保函数 _b.a
被调用与这个
设置为全局对象(或如果严格模式已启用,未定义
)。如果要直接调用 _b.a()
,那么 _b.a
将以这个
设置为 _b
。
(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)();
相当于
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).
这篇关于为什么babel重写导入的函数调用(0,fn)(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!