console.log()打印出函数

console.log() prints out function

本文关键字:函数 打印 log console      更新时间:2023-09-26

我正在CoffeeScript和Rails中做一个新项目,但当我在CoffeeSript中编写一些类时,我遇到了一些问题。

我正在做以下事情:

# 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,但没有回应。

如果这里有什么有用的东西,我会很高兴的。

Coffescript编译为JavaScript,因此在浏览器中无法使用漂亮的Coffeescrapt语法。在没有括号的情况下调用_formatTime,您将得到函数的定义。要获得您要查找的内容,您需要将其称为_formatTime()