Fix Object.assign example
See original GitHub issueThe Object.assign
example is currently incorrect.
function createMenu(config) {
Object.assign(config, {
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
});
}
createMenu({ title: 'Not Foo' });
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):
function createMenu(config) {
// makes a copy of `config`, with default values
config = Object.assign({
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
}, config);
}
How you could do it with mutation (this is identical to the “bad” example):
function createMenu(config) {
// mutates `config`, setting default values
Object.assign(config, Object.assign({
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
}, config));
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:6
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Object.assign() - JavaScript - MDN Web Docs
The Object.assign() method copies all enumerable own properties from one or more source objects to a target object.
Read more >Object spread vs. Object.assign - javascript - Stack Overflow
For example, when spreading an array null values are spread. ... The fix? Use Object.assign to not discard the previous object, but to...
Read more >ES6 Object.assign() Sample
Sample illustrating the use of ES6 Object.assign(). ... Object.assign() copies the values (of all enumerable own properties) from one or more source objects ......
Read more >ECMAScript 6: merging objects via Object.assign() - 2ality
Copying all properties of one object to another one is a common operation in JavaScript. This blog post explains ECMAScript 6's implementation ...
Read more >JavaScript Object.assign() - eduCBA
Javascript Object.assign() method is mainly used to copy the variable values and its properties from one or more source objects to the destination...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@rkichenama that example does not work because
config
has already mutated when the ‘overrides’ are applied:Hence the need for an additional
Object.assign
.@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 https://github.com/ryanmcdermott/clean-code-javascript/commit/d00ca4c70c97076b9678c193b125fdf9b810b89e with @kwelch’s version.
@connec I agree ours are the same, I just prefer not editing parameters if you are attempting to be functional.