我在CoffeeScript和Rails中做了一个新项目,但是当我在CoffeeScript中编写一些类时,我坚持了一些东西。
我正在做以下事情:
# Definition of functions like every, after here...
class Tracker
constructor: ->
@currentTime = 0
@_updateTime(@currentTime)
every 1000, @_countTime.bind @ # Just a shorthand for setInterval
_countTime: ->
time = @currentTime + 1
@_updateTime(time)
_updateTime: (time) ->
@currentTime = time
@_formatTime
_formatTime: ->
t = @currentTime
seconds = t % 60
minutes = (t / 60) % 60
hours = t / 3600
return seconds + minutes + hours
但是当我打印出来时(使用console.log
)函数_formatTime
的返回值我将打印出函数。
我用谷歌搜索,但没有发现任何有用的东西。检查了#coffeescript的IRC,但没有回复。
很高兴在这里有所帮助。
答案 0 :(得分:2)
Coffescript编译为JavaScript,所以在浏览器中你不能使用漂亮的Coffeescript语法。你在没有括号的情况下调用_formatTime
,你将获得函数的定义。要获得您想要的内容,您需要将其称为_formatTime()