javascript - Using a rest parameter before the last parameter in function definition -
my problem best explained simple code example:
function dothing(a, ...b, c) { console.log(a, b, c) } dothing(1,2,3,4); // expect "1 [2,3] 4" this instead gives syntax error unexpected token, pointing comma after b in function definition.
is not possible put single parameter after 'rested' parameter es6? if not, idea why? useful me. thanks!
edit: thought dothing(a, ...b, c) unambiguous, see dothing(1,2,3) need arbitrary rule decide whether 3 goes in b or c (i.e. if pass number of params less or equal number of params in function definition).
- you should not use js keywords in function names (e.g.
do) - as developer.mozilla.org site says:
if last named argument of function prefixed ..., becomes array elements 0 theargs.length supplied actual arguments passed function.
actually feature of es6 called rest parameters meant last in list of parameters.
so code work:
function do2 (a, b, ...c) { console.log(a, b, c); } do2(1,2,3,4);
Comments
Post a Comment