I just saw a code snippet in MDN about the destruction of rest parameters like this:
function f(...[a, b, c]) {
return a + b + c;
}
f(1)
f(1, 2, 3)
f(1, 2, 3, 4)
The code snippet is on this page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
Although the general use case for rest parameters is very clear to me ( function foo(...params){/*code*/}), I could not think of real use for rest parameters, such as the method presented in this code fragment. Instead, I think that in this case I should just use a generic function definition:
function f(a, b, c) {
return a + b + c;
}
f(1)
f(1, 2, 3)
f(1, 2, 3, 4)
source
share