New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Object.assign example #4
Comments
Sorry if I'm overthinking it, but I find it rather confusing to provide the example of object mutation in a procedural style, while advising to favor functional programming over imperative programming. |
If the purpose is to mutate function createMenu(config) {
Object.assign(
config, // object to merge changes into
{
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
}, // defaults
config // overrides
);
}
createMenu({ title: 'Not Foo' }); IMHO, I think this is the case since there is no return and we are dealing with objects, passed by reference. |
I would say the best option would be to maintain the passed in parameter and create a new object all together that use defaults then user param. function createMenu(options) {
let config = Object.assign({}, {
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
}, // defaults
options // overrides
);
} |
Good have to return the be object `config`
…On Thu, Jan 5, 2017, 15:43 Kyle Welch ***@***.***> wrote:
I would say the best option would be to maintain the passed in parameter
and create a new object all together that use defaults then user param.
function createMenu(options) {
let config = Object.assign({}, {
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
}, // defaults
options // overrides
);
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#4 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABjwmaSKZ3XshXMv1qIBTXxz5dE_Yfwdks5rPVXjgaJpZM4La-SD>
.
|
@rkichenama that example does not work because let config = { key: 'override' }
Object.assign(config, { key: 'default' }, config)
console.log(config.key) // default Hence the need for an additional @kwelch that option is equivalent to the non-mutating version I specified - the defaults are already in a new object, so merging them into another new object is redundant. Anyway, looks like this was fixed in d00ca4c with @kwelch's version. |
@connec I agree ours are the same, I just prefer not editing parameters if you are attempting to be functional. |
The
Object.assign
example is currently incorrect.The
title
property will always be set to'Foo'
!I was going to make a PR with a fix, but the approach really depends on whether the mutation of
config
is desirable.How I would do it (this does not behave identically to the "bad" example):
How you could do it with mutation (this is identical to the "bad" example):
The text was updated successfully, but these errors were encountered: