Javascript arguments on ES2015 Arrow functions
Javascript function arguments can be accessed by functions defined using the function
keyword such as:
function logArgsES5 () {
console.log(arguments)
}
logArgsES5('foo', 'bar')
// => Arguments(2) ["foo", "bar"]
But ES2015 Arrow functions does not bind this variable, so if you try this you will see an error:
let logArgsES2015 = () => {
console.log(arguments)
}
logArgsES2015('foo', 'bar')
// => Uncaught ReferenceError: arguments is not defined
So if we want to have similar variable we can add an ...arguments
as the function argument:
let logArgsES2015 = (...arguments) => {
console.log(arguments)
}
logArgsES2015('foo', 'bar')
// => Array(2) ["foo", "bar"]
Tweet