Objects

  • 3.1 Use the literal syntax for object creation. eslint: no-new-object

    1. // bad
    2. const item = new Object();
    3. // good
    4. const item = {};

  • 3.2 Use computed property names when creating objects with dynamic property names.

    Why? They allow you to define all the properties of an object in one place.

    ```javascript

    function getKey(k) {
    return a key named ${k};
    }

    // bad
    const obj = {
    id: 5,
    name: ‘San Francisco’,
    };
    obj[getKey(‘enabled’)] = true;

    // good
    const obj = {
    id: 5,
    name: ‘San Francisco’,

  1. [getKey('enabled')]: true,
  2. };
  3. ```

  • 3.3 Use object method shorthand. eslint: object-shorthand

    1. // bad
    2. const atom = {
    3. value: 1,
    4. addValue: function (value) {
    5. return atom.value + value;
    6. },
    7. };
    8. // good
    9. const atom = {
    10. value: 1,
    11. addValue(value) {
    12. return atom.value + value;
    13. },
    14. };

  • 3.4 Use property value shorthand. eslint: object-shorthand

    Why? It is shorter to write and descriptive.

    1. const lukeSkywalker = 'Luke Skywalker';
    2. // bad
    3. const obj = {
    4. lukeSkywalker: lukeSkywalker,
    5. };
    6. // good
    7. const obj = {
    8. lukeSkywalker,
    9. };

  • 3.5 Group your shorthand properties at the beginning of your object declaration.

    Why? It’s easier to tell which properties are using the shorthand.

    1. const anakinSkywalker = 'Anakin Skywalker';
    2. const lukeSkywalker = 'Luke Skywalker';
    3. // bad
    4. const obj = {
    5. episodeOne: 1,
    6. twoJediWalkIntoACantina: 2,
    7. lukeSkywalker,
    8. episodeThree: 3,
    9. mayTheFourth: 4,
    10. anakinSkywalker,
    11. };
    12. // good
    13. const obj = {
    14. lukeSkywalker,
    15. anakinSkywalker,
    16. episodeOne: 1,
    17. twoJediWalkIntoACantina: 2,
    18. episodeThree: 3,
    19. mayTheFourth: 4,
    20. };

  • 3.6 Only quote properties that are invalid identifiers. eslint: quote-props

    Why? In general we consider it subjectively easier to read. It improves syntax highlighting, and is also more easily optimized by many JS engines.

    1. // bad
    2. const bad = {
    3. 'foo': 3,
    4. 'bar': 4,
    5. 'data-blah': 5,
    6. };
    7. // good
    8. const good = {
    9. foo: 3,
    10. bar: 4,
    11. 'data-blah': 5,
    12. };

  • 3.7 Do not call Object.prototype methods directly, such as hasOwnProperty, propertyIsEnumerable, and isPrototypeOf.

    Why? These methods may be shadowed by properties on the object in question - consider { hasOwnProperty: false } - or, the object may be a null object (Object.create(null)).

    1. // bad
    2. console.log(object.hasOwnProperty(key));
    3. // good
    4. console.log(Object.prototype.hasOwnProperty.call(object, key));
    5. // best
    6. const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
    7. /* or */
    8. import has from 'has'; // https://www.npmjs.com/package/has
    9. // ...
    10. console.log(has.call(object, key));

  • 3.8 Prefer the object spread operator over Object.assign to shallow-copy objects. Use the object rest operator to get a new object with certain properties omitted.

    1. // very bad
    2. const original = { a: 1, b: 2 };
    3. const copy = Object.assign(original, { c: 3 }); // this mutates `original` ಠ_ಠ
    4. delete copy.a; // so does this
    5. // bad
    6. const original = { a: 1, b: 2 };
    7. const copy = Object.assign({}, original, { c: 3 }); // copy => { a: 1, b: 2, c: 3 }
    8. // good
    9. const original = { a: 1, b: 2 };
    10. const copy = { ...original, c: 3 }; // copy => { a: 1, b: 2, c: 3 }
    11. const { a, ...noA } = copy; // noA => { b: 2, c: 3 }