CoffeeScript and NodeJS: How to export multiple classes?

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

 class Dog bark: -> console.log "Arff! :D" class Cat meaow: -> console.log "Meaw!" module.exports = {Dog, Cat} 

How can I do something like this without typing class names twice?

+7
source share
3 answers

You can use something like this:

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

Compiles to:

 exports.Dog = (function() { function Dog() {} Dog.prototype.bark = function() { return console.log("Arff! :D"); }; return Dog; })(); 
+5
source

An alternative way to do this is to do 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 Animals.dog() 
+3
source

In general, I want a local variable (so I donโ€™t need to type exports.x all the time) and an export variable (so I donโ€™t need to define all export data at the end), so I follow these steps:

 exports.dog = class Dog bark: -> exports.cat = class Cat meow: -> 
+1
source

All Articles