Reading a bit of Javascript code I have come across this:
router.route('/:id')
.put((...args) => controller.update(...args))
.get((...args) => controller.findById(...args));
Does anyone know what ...args means?
Reading a bit of Javascript code I have come across this:
router.route('/:id')
.put((...args) => controller.update(...args))
.get((...args) => controller.findById(...args));
Does anyone know what ...args means?
As a colleague commented to your question, they are Rest Parameters , where simply this (
...args
) is an object of typearray
that goes from0
toargs.length
, example:Complementing what @Naos points out, the "ellipses" are the Spread Operator , which allows you to specify that the function will treat the content of that parameter as an array, or more precisely an iterable.
Formerly that was done using
arguments
and in your question it would behave exactly the same: an array with all the parameters given to the function.However, the spread operator also allows you to do things that previously required a tricky loop by applying array_slice to arguments. For example, declaring a function for which you know its first parameter, but you don't know all the rest: