Fix default values overwriting data passed#9
Fix default values overwriting data passed#9BTMPL wants to merge 4 commits intoryanmcdermott:masterfrom BTMPL:patch-2
Conversation
In the "Set default objects with Object.assign" section your example would overwrite the data passed as a parameter with the default values. Changing the parameter order fixes the issue. Also, the original version was mutating the passed object, but this is non-issue now. Thanks to https://www.reddit.com/user/notNullOrVoid for spotting the mixed order.
|
@BTMPL since you've already created the PR I thought about, may I ask you to expand it a little bit? For the sake of demonstrating default parameters, I suggest to remove the fields I doubt that |
|
@stasgavrylov I've provided a more detailed example, please review: |
Still dont feel right, but it's more correct
|
much better, imo. although, I'd introduce new variable for assignment operation result to avoid parameter mutation. |
|
Thank you. There is no (apparent) downside of parameter mutation, other than if you need to access the original value for some reason. It wont mutate menuConfig. |
|
Thank you so much for looking at this! I've taken your PR as inspiration for this fixed and fully fleshed out example: d00ca4c |
|
In your new example, you introduce a dummy object to copy all of the params to. This adds unnecessary performance overhead. You're instantiating two object literals, and performing 2 copy operations: first from the defaults to the dummy object, then from the config to the dummy object. A fresh defaults object is instantiated every time this code is run so mutating it is a non issue. You only need to copy the config to the defaults object. There's no need for the dummy object. |
In the "Set default objects with Object.assign" section your example would overwrite the data passed as a parameter with the default values. Changing the parameter order fixes the issue.
Also, the original version was mutating the passed object, but this is non-issue now.
Thanks to https://www.reddit.com/user/notNullOrVoid for spotting the mixed order.