Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Latest commit

 

History

History
27 lines (20 loc) · 685 Bytes

prefer-object-literal.md

File metadata and controls

27 lines (20 loc) · 685 Bytes

prefer-object-literal

Object literal syntax, which initializes an object's properties inside the object declaration is cleaner and clearer than the alternative: creating an empty object, and then giving it properties one by one.

An issue is raised when the following pattern is met:

  • An empty object is created.
  • A consecutive single-line statement adds a property to the created object.

Noncompliant Code Example

var person = {}; // Noncompliant
person.firstName = 'John';
person.middleInitial = 'Q';
person.lastName = 'Public';

Compliant Solution

var person = {
  firstName: 'John',
  middleInitial: 'Q',
  lastName: 'Public',
};