objects sets of properties

An object is a set of properties, each with a name and a value.

Object Literals

Here is an example of an object.

d = {day: 4, month: "July"} 

This object has two properties named day and month.

day has the value 4 and month has the value "July"

The punctuation is important. Each property name must be followed by a colon : and the property value, and when listed on one line, the properties must be collected using curly braces and separated with commas.

Accessing Object Properties

Object properties are accessed using a dot . after the variable name. d.month refers to the value of the property month of the d object.

write d.month, d.day

Object properties can be changed just like variables.

d.day = 17

Multiline Object Literals

In CoffeeScript, object literals can be written on multiple lines.

If indented evenly, the curly braces and commas may be omitted. The following is equivalent to the code above:

d =
  day: 4
  month: "July"

You often see multiline object literals as arguments to functions. Here the css function uses the object properties to change the style of text on the page:

  background: "wheat"
  fontSize: "36px"
  textShadow: "0 0 5px"

The object's properties background, fontSize, and textShadow are standard CSS property names.

New Constructed Objects

Special classes of objects can also be created using new:

t = new Turtle
write t.length
t.fd 100

Turtle is a predefined class: when you new Turtle, it creates an object with many properties such as length (how many turtles is just one) and fd (a function that moves the turtle forward).

Function properties like fd are called methods.