How to export multiple classes from a single file?

2019-07-30 16:45发布

问题:

Say I have a file called participant.coffee, with two classes in it:

class ParticipantData
     constructor: (data) ->
           # whatever

     doSomething:
          console.log 'participant data!'

class ParticipantResult
     doAnotherThing:
          console.log 'participant result!'

module.exports = new ParticipantResult()

Right now I can access ParticipantResult by using require('.particpantresult'), but I can't figure out any way to call ParticipantData's constructor. Is it possible to access ParticipantData without moving it to it's own file?

回答1:

return an object with the constructors, then call those:

module.exports = {
  ParticipantData: ParticipantData,
  ParticipantResult: ParticipantResult
};

with the class-using code:

var dm = require("...");
var myParticipantResult = new dm.ParticipantResult();