CoffeeScript and NodeJS: How do I export multiple classes?


I want to export some classes, say, Dog and Cat. One way to go about this is:

class Dog
  bark: -> console.log "Arff! :D"

class Cat
  meaow: -> console.log "Meaw!"

module.exports = {Dog, Cat}

How can I do something similar without typing the class names twice?

Problem courtesy of: Andres Riofrio


You could use something like this:

class exports.Dog
  bark: -> console.log "Arff! :D"

This compiles down to:

exports.Dog = (function() {

  function Dog() {}

  Dog.prototype.bark = function() {
    return console.log("Arff! :D");

  return Dog;

Solution courtesy of: jimr


Generally I want a local variable (so I don't have to type exports.x all the time) and an export variable (so I don't have to define all the exports at once in the end), so I do the following: = class Dog
  bark: -> = class Cat
  meow: ->
Discussion courtesy of: Jonathan Ong

An alternative way to this is by doing the following:

module.exports = 
   Dog: class Dog
          bark: -> console.log "Arff! :D"

   Cat: class Cat
         meaow: -> console.log "Meaw!"

Then you can do the following:

animals = require './animals'

dog = new
Discussion courtesy of: Menztrual

This recipe can be found in it's original form on Stack Over Flow.