small medium large xlarge

Back to: All Forums  CoffeeScript
Generic-user-small
20 Sep 2011, 21:55
Nathan Benes (1 post)

As I’m going through the examples in the book there were some stylistic choices that I wanted to do differently (partly prove to myself that I can do more than just copy-paste the example code and actually get things working for myself), but I’m running into an odd issue that I don’t…understand. Maybe someone can enlighten me :)

Instead of making tileValues = {hash} a module-scoped variable in the player.coffee file, I wanted to add it as a class variable on the Dictionary object so that (from player) one could call Dictionary.tileValues[letter]:

Note: ‘…’ means I cut out parts of the code for brevity

dictionary.coffee snippet:

class Dictionary
  #...
  @tileValues:
    A: 1, B: 3.... (etc etc)
  #...

player.coffee snippet:

class Player
  #...
  scoreMove: (swapCoordinates) ->
    #...
    for word in words (...)
      moveScore += Dictionary.tileValues[letter] for letter in word
      newWords.push word
  #...

But when I run this (using the console version that requires each file, and using the browser version that includes each file in the head tag) I get the error:

Dictionary is undefined

With some arbitrarily difficult-to-debug line (I finally narrowed it down but there’s got to be an easier way to see where an error is happening o.O)

I noticed for the console version that if I add this to the very top of player.coffee, it works fine:

if exports? then {Dictionary} = require './dictionary'

But I don’t feel like this should be necessary because that file was already required from console.coffee.

Additionally in the browser version after it gives me that error, I can still open up my firebug console and get this result:

>>> Dictionary
Dictionary(originalWordList, grid)

indicating that it does indeed know what the Dictionary object is, just apparently not when called from the player file (but why not?? That seems to break the purpose of defining classes in separate files and then ‘exporting’ them into the root window object)

What have I done wrong here?

You must be logged in to comment