如何在节点中使用coffeescript导出类

时间:2015-04-21 09:04:41

标签: javascript node.js coffeescript

文件1:

module.exports = {
    class Verify
      myMethod: ->
        # return stuff

和file2:

Verify    = require('excelTest.js')
verify    = new Verify
verify    = verify.myMethod()

以上失败:

Running "coffee:glob_to_multiple" (coffee) task
>> server/api/abrechnung/excelTest.coffee:7:2: error: unexpected class
>>  class Verify
>>  ^^^^^
>> In file: server/api/abrechnung/excelTest.coffee
>> On line: 6
>>  class Verify
>>  ^

1 个答案:

答案 0 :(得分:2)

module.exports.Verify = class Verify
  mymethod: -> 
  ..

或者,可能更具可读性,将类定义及其导出分开:

class Verify
  mymethod: ->
  ..

module.exports =
  Verify: Verify

在这两种情况下,它都是require('excelTest.js').Verify;如果您只是想要Verify,请忽略额外的require('excelTest.js')