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 function arguments. This makes the options easier to understand from the code calling the function because there are keys to identify what each value does. It also alleviates the problems of keeping track of arguments and argument order...