Comments (6)
@rkichenama that example does not work because config
has already mutated when the 'overrides' are applied:
let config = { key: 'override' }
Object.assign(config, { key: 'default' }, config)
console.log(config.key) // default
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 d00ca4c with @kwelch's version.
from clean-code-javascript.
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.
from clean-code-javascript.
If the purpose is to mutate config
to add defaults but keep changes, then the example should be
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.
from clean-code-javascript.
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
);
}
from clean-code-javascript.
from clean-code-javascript.
@connec I agree ours are the same, I just prefer not editing parameters if you are attempting to be functional.
from clean-code-javascript.
Related Issues (20)
- Bonne pratique JS HOT 1
- J HOT 1
- Farsi translation
- The code example of <Don't ignore rejected promises> may be wrong.
- Nice HOT 4
- Contradict HOT 1
- De code HOT 5
- Js two HOT 1
- eslint plugin
- Add OSSF Scorecard security workflow
- Clean code HOT 1
- Close Issues with no description/details attached. HOT 1
- J HOT 1
- J
- I
- Clean HOT 2
- Js HOT 1
- Clean HOT 1
- Adding translations HOT 3
- CleanCode HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clean-code-javascript.