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.

Tip

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 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.