gpt4 book ai didi

javascript - Coffeescript,导出类只有超函数

转载 作者:行者123 更新时间:2023-12-02 18:30:26 25 4
gpt4 key购买 nike

给定

class Super:

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

class Child extends Super:

internalFoo = ->
...

internalBar : ->
...


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

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

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

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

最佳答案

我可以毫无问题地调用 internalBar

internalFoo 无法被调用,因为您没有将其定义为类的成员函数,也没有定义为类函数。通过 "=" 定义它将使其只能作为变量在类主体内关闭。

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

 class Child extends Super
@internalFoo: ->

至于无法调用internalBar,我假设您有缩进错误,并且您的internalBar被定义为internalFoo中的匿名函数。正如之前所说,它不可用,因为您尚未将其定义为成员方法。

关于javascript - Coffeescript,导出类只有超函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17853033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com