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?
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;
})();
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 Animals.dog()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With