-
Book Overview & Buying
-
Table Of Contents
CoffeeScript Application Development
By :
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 omit the curly braces. This is possible on both the single-line and multi-line versions.
author = name: "Ian", age: 26, favoriteLanguage: "CoffeeScript" authorsBicycle = color: "black" fenders: true gears: 24
This even works with nested objects...
Change the font size
Change margin width
Change background colour