Javascript es6: use case for restructuring rest parameter

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)          // NaN (b and c are undefined)
f(1, 2, 3)    // 6
f(1, 2, 3, 4) // 6 (the fourth parameter is not destructured)

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)          // NaN (b and c are undefined)
f(1, 2, 3)    // 6
f(1, 2, 3, 4) // 6 (the fourth parameter is not defined)
+6
source share
2 answers

function f(a, b, c) { … } . + , , .. f.length == 0.

rest. , , - . , MDN .

+4

, , .

, TypeScript, Babel , , .

0

All Articles