Static Properties

How about defining class (i.e., static) properties? Well, it turns out that inside a class definition, this refers to the class object. In other words, you can set class properties by setting them directly on this.

class Animal
  this.find = (name) ->      

Animal.find("Parrot")

In fact, as you may remember, CoffeeScript aliases this to @, which lets you write static properties even more succinctly:

class Animal
  @find: (name) ->

Animal.find("Parrot")

Get The Little Book on CoffeeScript 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.