A new idiom: options objects

Now we're going to look at an idiom for passing options to a function. This isn't a new feature of CoffeeScript. Instead, it's a convention that makes use of several CoffeeScript features we've already learned, and uses them in a pattern that is easy to understand and useful in a wide variety of situations.


This idiom is also common in Ruby programs. Ruby makes extensive use of hashes (the equivalent of simple objects), and has loose syntax rules (much like CoffeeScript) allowing hashes to be passed to functions without extra noise. Other languages, such as Python, offer similar benefits via named arguments.

The idea is simple: a function accepts an options object, which may contain keys for any less-common or less-obvious ...

Get CoffeeScript Application Development now with O’Reilly online learning.

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