1

It's really likely I won't get any positive answer to my question, but I'll ask anyway.

I have a function taking an object parameter.

function myFunc(options) {}

This object contains properties that I'd like to assign default values to. Using destructuring, I can easily do this:

function myFunc({
  mandatory_field, // Without default value
  i = 1,
  j = 2,
  k = 3,
  obj: {
    prop1 = 'Val',
    prop2 = 2
  } = {},
  str: 'Other val'
} = {}) {}

Now, inside the function, I have all these variables with either their default value or the ones passed, which is great.
Howevever, I need to pass this options object to other functions, so in the end, I'll probably need to rebuild it that way:

function myFunc({
  mandatory_field, // Without default value
  i = 1,
  j = 2,
  k = 3,
  obj: {
    prop1 = 'Val',
    prop2 = 2
  } = {},
  str: 'Other val'
} = {}) {
  let options = {
    mandatory_field,
    i,
    j,
    k,
    obj: {
      prop1,
      prop2
    },
    str
  };
  otherFunc1(options);
  otherFunc2(options);
}

But this feels really redundant.

I thought aliasing the destructured object would work, but it doesn't.
Is there any cleaner way anyone could think of to do this?

Jerska
  • 11,722
  • 4
  • 35
  • 54
  • 1
    I don't think there is a much better way to do this. You can only make use of the default values when using destructuring, in which case you "loose" the reference to the whole object. You need to rebuild the object to get the default values. – Felix Kling Jan 07 '16 at 15:02
  • Do you really need all keys on that object in all three functions? What is the use case of running three functions with the same parameters? Maybe you can refactor the functions to only use some parameters and not others? – Mario Tacke Jan 07 '16 at 17:18
  • You can't do this with destructuring. But you can use `Object.assign` to achieve the result: you'd have both the options object and the individual variables. If you're interested in a snippet, let me know and I'll post an answer. – Esteban Jan 07 '16 at 18:00
  • Actually, you can't, since `Object.assign` will not merge object properties. – Esteban Jan 07 '16 at 18:02
  • @MarioTacke I indeed need to. I have a single entry point accepting an object with all the needed parameters. Some of them are global (e.g. colors), some of them more specific (size of some stuff that I'll call `foo`). I basically need to send to the function handling `foo`'s logic both the globals and its specific config. – Jerska Jan 07 '16 at 20:12
  • 1
    @Esteban It seems like my best option is using something like [`_.defaultsDeep`](https://lodash.com/docs#defaultsDeep). – Jerska Jan 07 '16 at 20:13
  • 1
    It probably is. Merging deeply requires making some choices about how that should happen and what you want to protect against, which is probably why there's not and probably never will be a single "right" native method. See this answer: http://stackoverflow.com/a/28248548/1971194 – Dtipson Feb 09 '16 at 02:08
  • Thanks for this link @Dtipson , it indeed helped me understand the issues involved here. I'd be happy to accept this as an answer. – Jerska Feb 09 '16 at 14:57

0 Answers0