1const defaultConfig = {
2 server: { host: 'localhost', port: 3000 },
3 database: { host: 'localhost', port: 5432 },
4};
5
6function createConfig(overrides) {
7 const config = Object.assign({}, defaultConfig, overrides);
8 return config;
9}
10
11const myConfig = createConfig({});
12myConfig.server.port = 8080;
13// defaultConfig.server.port is now 8080 too
no lines flagged
#039PracticeMedium15 min · 120 XP
Object.assign Shallow Copies Nested Objects
Modifying a cloned config object also mutates the original because the clone is shallow.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.