为什么babel重写导入的函数调用(0,fn)(...)? [英] Why does babel rewrite imported function call to (0, fn)(...)?

查看:133
本文介绍了为什么babel重写导入的函数调用(0,fn)(...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个输入文件,例如

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();

是逗号运算符,请参阅 https://developer.mozilla.org/en- US / docs / Web / JavaScript / Reference / Operators / Comma_Operator )。

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

这篇关于为什么babel重写导入的函数调用(0,fn)(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆