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).

  1. you should not use js keywords in function names (e.g. do)
  2. 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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -