Adrian Silvescu
Adrian Silvescu

Reputation: 844

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

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.

Upvotes: 6

Views: 8153

Answers (2)

guest271314
guest271314

Reputation: 1

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

Upvotes: 10

Dmitry Druganov
Dmitry Druganov

Reputation: 2368

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

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

Upvotes: 2

Related Questions