duber
duber

Reputation: 2869

Cannot enter CoffeeScript for loop

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...

Upvotes: 0

Views: 54

Answers (1)

mu is too short
mu is too short

Reputation: 434685

Probably because @columns is undefined.

Your constructor:

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

simply creates an object and throws it away, it is the same as this:

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

So no instance variables are set and your constructor doesn't do much at all. Perhaps you meant to say:

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

or possibly:

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

I'm assuming that your createBoard method is actually indented one level so that it is a method in your Game class.

Upvotes: 2

Related Questions