Coffeescript,出口类只有超级功能

时间:2013-07-25 08:39:21

标签: javascript coffeescript

鉴于

class Super:

    constructor: (@params) ->
    foo: ->
        ...

class Child extends Super:

    internalFoo = ->
        ...

    internalBar : ->
        ...


(exports ? this).Super = Super
(exports ? this).Child = Child

我知道这个例子会“污染”全局命名空间,但为了简单起见:我只能在foo()上调用Child

var c = new Child();
c.foo(); // works
c.internalFoo(); // internalFoo is undefined
c.internalBar(); // internalBar is undefined

我在这里缺少什么?我想在浏览器中运行我的coffeescripted代码。

1 个答案:

答案 0 :(得分:2)

我可以毫无问题地致电 internalBar

无法调用

internalFoo ,因为您没有将其定义为类的成员函数,也不能将其定义为类函数。通过“=”定义它将使它仅作为变量在类体内关闭。

如果你想让它成为一个类方法,请将其定义为

 class Child extends Super
     @internalFoo: -> 

至于无法调用 internalBar 我会假设您有缩进错误, internalBar 被定义为 internalFoo 中的anon函数。 如前所述,因为您没有将其定义为成员方法。

相关问题