所以我正在为一个项目编写一些coffeescript,并试图在类中创建一些静态属性。我一直在关注代码库中的另一个文件,它成功地做了同样的事情,但我的文件不起作用。
我的代码
class Messages
@toggleUnreadConversations:()->
# This is the line in question, Messages is defined with all the
# functions but the property ViewOnlyUnread is undefined
Messages.ViewOnlyUnread = !Messages.ViewOnlyUnread
@init:->
@ViewOnlyUnread = false
代码库中成功使用静态属性的其他代码
class Map
@CacheRealtor: (realtor) ->
realtor.realtor_id = parseInt(realtor.realtor_id)
# Here the static property IdToRealtorMap is defined
Map.IdToRealtorMap[parseInt(realtor.realtor_id)] = new Realtor()
@Init: ->
@IdToListingMap = []
@IdToRealtorMap = []
根据我所知,这些init函数的调用方式与页面加载时调用init的方式相同。这两个类都是静态类,从来没有创建它们中任何一个的实例。有人知道可能是什么问题吗?
init
函数正在设置一个实例变量,但toggleUnreadConversations
函数试图将其引用为类的属性。
您应该使用@
来引用init
设置的实例变量:
class Messages
@toggleUnreadConversations: ->
# reference the instance variable
@ViewOnlyUnread = !@ViewOnlyUnread
@init: ->
@ViewOnlyUnread = false