0

In a project I have this BabelJS configuration:

{
  "presets": ["es2016"]
}

And somewhere in the code there is a method using the standard JS 'arguments' keyword to deal with a dynamic range of parameters. Something like this:

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

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

So far so good but when BabelJS is done, it has (1) defined 'arguments' as a global variable and (2) also in a way that is bound to fail in itself:

var arguments = arguments;

Is there a way to stop BabelJS from doing this? Either by configuration or some special comment to make exceptions?

Jos
  • 419
  • 3
  • 12

1 Answers1

3

Don't use the magic arguments variable.

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

If you, for some reason, must use the arguments variable (hint, you almost always don't), you have to use .apply() like you would before ES5 was a thing.

(function() {
  someOtherFunction.apply(null, arguments);
})
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Great! Thank you @Madara! – Jos Feb 01 '18 at 10:16
  • Yes I didn't want to use 'apply' although that is a possibility. I'm happy with your answer to avoid the issue but I still wonder why BabelJS doesn't consider 'arguments' as the keyword that it is and creates a global variable. – Jos Feb 01 '18 at 10:28
  • 2
    @Jos Arrow functions don't have arguments `var test = ()=>console.log(arguments);test()` so babel probably assumes that `arguments` is a global variable – HMR Feb 01 '18 at 10:46