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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.