Use computed property names when creating objects with dynamic property names.
```javascript
function getKey(k) {
returna key named ${k}
;
}// bad
const obj = {
id: 5,
name: ‘San Francisco’,
};
obj[getKey(‘enabled’)] = true;
[getKey('enabled')]: true,
};
```
3.3 Use object method shorthand. eslint:
3.4 Use property value shorthand. eslint:
const lukeSkywalker = 'Luke Skywalker';
// bad
const obj = {
lukeSkywalker: lukeSkywalker,
};
// good
lukeSkywalker,
3.5 Group your shorthand properties at the beginning of your object declaration.
Only quote properties that are invalid identifiers. eslint:
quote-props
// bad
const bad = {
'foo': 3,
'bar': 4,
'data-blah': 5,
};
// good
const good = {
foo: 3,
bar: 4,
};
3.7 Do not call
Object.prototype
methods directly, such ashasOwnProperty
, , andisPrototypeOf
.-
// very bad
const original = { a: 1, b: 2 };
const copy = Object.assign(original, { c: 3 }); // this mutates `original` ಠ_ಠ
delete copy.a; // so does this
// bad
const original = { a: 1, b: 2 };
const copy = Object.assign({}, original, { c: 3 }); // copy => { a: 1, b: 2, c: 3 }
// good
const original = { a: 1, b: 2 };
const copy = { ...original, c: 3 }; // copy => { a: 1, b: 2, c: 3 }
const { a, ...noA } = copy; // noA => { b: 2, c: 3 }