访问类定义之外的变量

时间:2015-03-08 01:11:39

标签: class coffeescript

我需要访问类定义之外的变量,如下例所示。

module.exports = (config) ->
    class Foo
        constructor: () ->
            console.log config

    return Foo

但是如果我尝试创建该类的实例,则该变量未定义且无法打印。

Foo = require('./foo.coffee')(config)
bar = new Foo()

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

最好试试这个:

module.exports = 
    class Foo
        constructor: ( @config ) ->
            console.log @config

调用看起来像这样:

Foo = require('./foo.coffee')
bar = new Foo(config)

在这种情况下我使用了@config,但您也可以使用config