arrow functions: destructuring arguments and grabbing them as a whole at the same time

Viewed 5419

Can we do both destructuring: ({a, b}) => ( a + b ) and grab the arguments: (...args) => ( f({...args}) ), at the same time for arrow functions in ES6.

Looking for something like (...args = {a, b}) => ( a + b + f({...args}) ).

My curent solution is to do something like:

({a, b}) => {
  const {...args} = {a, b}
  return a + b + f({...args})
}

But it is redundant or (thanks to nnnnnn & Dmitry)

(args) => {
  const {a, b} = args
  return a + b + f({...args})
}

which is less redundant and definitely better but still not entirely satisfactory.

2 Answers

You can use default parameters and object rest at target of destructuring assignment for first parameter, where a single object is expected, and optionally define subsequent parameters by destructuring previously defined object parameter

const fn = ({...args} = {}, {a, b} = args) => console.log(a, b, args);

If you mean that f also accepts named parameters, so why not just use:

(params) => {
  const { a, b } = params
  return a + b + f(params)
}
Related