无法读取属性'长度'未定义的

时间:2013-08-15 18:56:33

标签: javascript jquery class coffeescript

$ ->
   window.app.helpers = new class Helpers

    constructor: (@name) ->
      @slideRecipeId     = $("#slideRecipe")
      @specialities      = ["A", "B", "C"]

      setTimeout @countUpRecipes, 3000

    countUpRecipes: ->
      @slideRecipeId.html(@specialities[Math.floor(Math.random() * @specialities.length)])

问题是在加载时我遇到了这些错误:

Uncaught TypeError: Cannot read property 'length' of undefined 

代码有什么问题?感谢。

1 个答案:

答案 0 :(得分:1)

您需要在调用@countUpRecipes时绑定上下文:

setTimeout => 
    @countUpRecipes
, 3000