Can somebody explain to me, in detail, how the spread operator works when used to get the parameters of an input function (function passed in as a parameter to another function)?
For example, consider a simple throttle
function:
const throttle = (func, wait) => {
let halt = false
return function(...args) {
if (!halt) {
func.call(this, ...args)
halt= true
setTimeout(()=>halt=false, wait)
}
}
}
In the above sample, ...args
is used twice, once as the parameter of the inner arrow function, and the other time is passed as parameters to the func
call.
I am having a hard time understanding what ...args
is exactly. I know that if you use ...
on an array of items, it will simply deconstruct the array and spread them out to individual items. But what exactly is being spread here when the expected parameters are a function(probably with its own parameters) and a number? Also, when using the apply
on func
, why are we spreading again?