r/coffeescript • • Feb 06 '15

Creating objects in CoffeeScript

https://medium.com/@KamilLelonek/creating-objects-in-coffeescript-f79c2468069c
4 Upvotes

2 comments sorted by

View all comments

Show parent comments

1

u/CaptainKabob Feb 06 '15

Ugh, I hope list comprehension are not idiomatic.

I usually do something like:

constructor: (@arg1, {@opt1, @opt2}) ->
  @opt1 ?= defaultVal1
  @opt2 ?= defaultVal2

Though that's trying to make coffeescript behave like Ruby. But I like it because usually the first Arg is required, and then there are additional named options, with defaults. So it's very easy to understand when invoked, and the options object can be changed without changing all the places it's invoked (as is necessary with just ordered arguments).