question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fix Object.assign example

See original GitHub issue

The 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:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
conneccommented, Jan 5, 2017

@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 https://github.com/ryanmcdermott/clean-code-javascript/commit/d00ca4c70c97076b9678c193b125fdf9b810b89e with @kwelch’s version.

0reactions
kwelchcommented, Jan 6, 2017

@connec I agree ours are the same, I just prefer not editing parameters if you are attempting to be functional.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found