Why does Babel add the following line of code when transpiling my javascript from ES5 to ES2015?
var _arguments = arguments;
This line breaks the functionality in my code that refers to arguments...Any ideas?
The function that breaks, simply takes one string, 'word' and concats it to the following argument passed in:
concatWordsExample: word => {
let wordAndFirstArg = word + arguments ? arguments[0] : '';
}
Example of function call:
concatWordsExample('firstword ', 'secondword');
// I expected wordAndFirstArg to be 'firstword secondword';
I am using Babel v6.0.0 via Grunt
The problem here was that I was using 'arguments' (ES5 syntax) within an ArrowFunction in my example:
concatWordsExample: word => {
let wordAndFirstArg = word + arguments ? arguments[0] : '';
}
Arguments in this case does not refer to the arguments of the arrow functions, which is what I wanted- "Any reference to arguments, super, or this within an ArrowFunction are resolved to their bindings in the lexically enclosing function." Hence, I could re-write to a traditional function, though the better solution was to simply use the new 'rest arguments' ES2015 syntax instead, for example:
concatWordsExample: (word, ...args) => {
let wordAndFirstArg = word + args ? args[0] : '';
}
Here is the official blurb from Babel:
"Any reference to arguments, super, or this within an ArrowFunction are resolved to their bindings in the lexically enclosing function. Even though an ArrowFunction may contain references to super, the function object created in step 4 is not made into a method by performing MakeMethod. An ArrowFunction that references super is always contained within a non-ArrowFunction and the necessary state to implement super is accessible via the scope that is captured by the function object of the ArrowFunction."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With