6. Objects

Understanding objects is crucial to writing well-organized, expressive JavaScript. If you’ve used other languages such as Java or ActionScript, JavaScript may seem like it’s missing a lot of language features such as classes and modules. But objects in JavaScript are flexible and powerful enough that you don’t often miss those features. Sometimes you can even emulate missing language features with clever uses of objects and functions.

Basics

The best way to create an object is to use an object literal.

var myObject = {};

You can create an object with properties by listing them in the object literal, separating property names and values with colons.

Tip

Notice that each property: value pair is separated by a comma, but make sure ...

Get The JavaScript PocketGuide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.