How to export multiple classes from a single file?

2019-07-30 16:20发布

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条回答
贪生不怕死
2楼-- · 2019-07-30 16:54

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();
查看更多
登录 后发表回答