Skip to content

Fix default values overwriting data passed#9

Closed
BTMPL wants to merge 4 commits intoryanmcdermott:masterfrom
BTMPL:patch-2
Closed

Fix default values overwriting data passed#9
BTMPL wants to merge 4 commits intoryanmcdermott:masterfrom
BTMPL:patch-2

Conversation

@BTMPL
Copy link

@BTMPL BTMPL commented Jan 4, 2017

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.

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.
@stasgavrylov
Copy link

stasgavrylov commented Jan 5, 2017

@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 title and buttonText from menuConfig completely, and change body and cancellable values to something different from default values. Because even with your fix, the first two will become null, and the last two remain unchanged.

I doubt that null values were intentional.

@BTMPL
Copy link
Author

BTMPL commented Jan 5, 2017

Still dont feel right, but it's more correct
@stasgavrylov
Copy link

much better, imo. although, I'd introduce new variable for assignment operation result to avoid parameter mutation.

@BTMPL
Copy link
Author

BTMPL commented Jan 5, 2017

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.

@ryanmcdermott
Copy link
Owner

Thank you so much for looking at this! I've taken your PR as inspiration for this fixed and fully fleshed out example: d00ca4c

@THEtheChad
Copy link

In your new example, you introduce a dummy object to copy all of the params to.

  config = Object.assign({}, {
    title: 'Foo',
    body: 'Bar',
    buttonText: 'Baz',
    cancellable: true
  }, config);

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments