Simple objects

You have no doubt spent a lot of time working with objects in JavaScript. They are the most versatile data structure in the language, and JavaScript encourages heavy use of them. CoffeeScript keeps all that great support for objects, and adds a few helpful features for dealing with them.

Declaring an object looks very familiar. Let's record a few biographical details:

author = { name: "Ian", age: 26 }

We can still access object properties like we do in JavaScript:

author.name
author["age"]
author.favoriteLanguage = "CoffeeScript"

Just like CoffeeScript arrays, if we declare the object properties on different lines, we can optionally omit the commas.

authorsBicycle = {
  color: "black"
  fenders: true
  gears: 24
}

That's not all! We can also ...

Get CoffeeScript Application Development 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.