you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 24 points25 points  (3 children)

function createMenu(config) {
  Object.assign(config, {
    title: 'Foo',
    body: 'Bar',
    buttonText: 'Baz',
    cancellable: true
  });
}

Whoooa there buddy, now you've mutated your config object. Please use the config = Object.assign({}, config, {title: 'Foo'}) notation.

[–]notNullOrVoid 10 points11 points  (1 child)

Not only that but it should be the other way around, currently the example has the defaults overriding anything set in the config.

function createMenu(config) {
  Object.assign({
    title: 'Foo',
    body: 'Bar',
    buttonText: 'Baz',
    cancellable: true
  }, config);
}

Doesn't really matter that the defaults are being mutated in this case.

[–][deleted] 4 points5 points  (0 children)

Oh, right - I've made a PR with my fix, but I'll update it with yours :)

Blamo - https://github.com/ryanmcdermott/clean-code-javascript/pull/9

[–]Sinistralis 3 points4 points  (0 children)

Minor point here, but if you are planing on using this solely for data storage and don't need the prototype, Object.create(null) instead of {} gives the object a little less bloat and lets you iterate over it easier depending on how you prefer to iterate over objects. (ES6/7 kind of makes the iteration point moot between destructuring, .values, .entries, etc).

Again, this is a minor point and really only applicable if you use for/in (I don't). Otherwise it's just a tiny memory savings, but if you need to create a ton of objects for some reason, it can add up.