我正在尝试组织我的代码,但遇到未定义的超类的问题。我希望这只是一个加载路径问题,但我无法弄清楚。我正在运行这个:
coffee rooms.coffee
房间.咖啡
Room = require('./rooms/room')
module.exports = class Rooms extends Object
constructor: ->
@
房间/房间.咖啡
module.exports = class Room
@Active: require('./active')
constructor: (@id) ->
@users = {}
房间/active.coffee
Room = require('./room')
console.log Room #=> {}
module.exports = class Active extends Room
constructor: (@id) ->
@type = "Active"
super
如果我尝试这样做new Active
,我会收到以下错误:
TypeError: Cannot read property 'constructor' of undefined
Active
的super
被列为undefined
:
[Function: Active] __super__: undefined
为什么Room
未定义?(或者更确切地说,只是一个空对象?)
更新
正如下面许多人指出的那样,这是由循环依赖引起的。我最终只是将子类定义放在基类定义中,而不是尝试将它们保存在单独的文件中。像这样的东西:
class Room
constructor: ->
# ...
class @Active extends Room
constructor: ->
# ...
class @Inactive extends Room
constructor: ->
# ...
active = new Room.Active
inactive = new Room.Inactive