JavaScript Literal Objects

Question
Should I ever use object literals and, if so, when should I use them?

Answer
Yes, you can use them. Use them when you want to create new objects without explicitly creating a new instance of Object.

Comments
Literal objects operate the same as non-literal objects. They just have a more compact syntax. Douglas Crockford recommends using object literals over the Object constructor.

Examples

// Not recommended.
let car = new Object();
car.make = "Honda";
car.model = "Prelude";

// Recommended.
let car = {
  make: "Honda",
  model: "Prelude"
};

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s