如何防止 BabelJS 生成 'arguments' 关键字的全局变量?

How to prevent BabelJS from making global variable of 'arguments' keyword?

在一个项目中我有这个 BabelJS 配置:

{
  "presets": ["es2016"]
}

在代码的某处有一个方法使用标准 JS 'arguments' 关键字来处理动态范围的参数。像这样:

const myFunction = () => {
  // pass all arguments with spread operator
  someOtherFunction(...arguments); 
}

myFunction('one', 'two', 3, {number: 4});

到目前为止一切顺利,但是当 BabelJS 完成后,它 (1) 将 'arguments' 定义为全局变量,并且 (2) 也以一种注定会失败的方式:

var arguments = arguments;

有没有办法阻止 BabelJS 这样做?是通过配置还是一些特殊的注释来做例外?

不要使用神奇的 arguments 变量。

(...args) => {
  // pass all arguments
  someOtherFunction(...args);
}

如果您出于某种原因必须使用 arguments 变量(提示,您 几乎总是 不用),则必须使用 .apply() 就像 ES5 出现之前一样。

(function() {
  someOtherFunction.apply(null, arguments);
})