Javascript log()输出函数
我正在用CoffeeScript和Rails做一个新项目,但当我用CoffeeScript编写一些类时,我遇到了一些困难 我正在做以下事情:Javascript log()输出函数,javascript,coffeescript,Javascript,Coffeescript,我正在用CoffeeScript和Rails做一个新项目,但当我用CoffeeScript编写一些类时,我遇到了一些困难 我正在做以下事情: # Definition of functions like every, after here... class Tracker constructor: -> @currentTime = 0 @_updateTime(@currentTime) every 1000, @_countTime.bind @ # Ju
# 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
)函数\u formatTime
的返回值时,我会打印出函数。
我在谷歌上搜索了一下,但没有发现任何有用的东西。也检查了#coffeescript的IRC,但没有回应
如果这里有什么有用的东西,我会很高兴的。Coffescript编译成JavaScript,所以在浏览器中不能使用漂亮的Coffescript语法。不带括号调用的
\u formatTime
,将得到函数的定义。要获取所需内容,您需要将其称为\u formatTime()
尝试在\u formatTime末尾添加括号。如果您只是console.log\u formatTime,则您正在打印包含函数的变量,而不是运行函数\u formatTime()