0

Why isn't my code hitting the line that contains the alert?

window.Game = class Game
  constructor: ->
    rows: 22
    columns: 10
    board: []

createBoard: ->                                                                                                                                                                                           
  # Some code here...
  for x in [0...@columns]
    alert("THIS IS HERE")
  # More code down here...
4

1 回答 1

2

大概是因为@columnsundefined

你的构造函数:

constructor: ->
  rows: 22
  columns: 10
  board: []

只需创建一个对象并将其丢弃,它与此相同:

constructor: ->
  o = {
    rows: 22
    columns: 10
    board: []
  }
  return

所以没有设置实例变量,你的构造函数根本没有做太多事情。也许你的意思是:

constructor: ->
  @rows = 22
  @columns = 10
  @board = []

或者可能:

constructor: (@rows = 22, @columns = 10, @board = [ ]) ->

我假设您的createBoard方法实际上是缩进一级,因此它是您Game类中的一种方法。

于 2013-07-01T02:00:47.910 回答