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?