0

I have looked around and have found a number of questions which approach using-a-string-to-define-the-class-name and dynamic-class-generation-in-coffeescript> but neither of them exactly address my problem, so I am wondering whether I making some fundamental mistake in my approach to the problem.

In the loop below I am looping through some data parsed from JSON. For each set of data I want to extend my class Robot with string = new Robot where string is a string.

Currently my code does not produce any errors, and successfully creates new Robots but since their name is a string, trying to access them with robot1.move() or robot2.doSomeOtherClassyThing() does not work and tells me they are undefined.

This seems like it should not require a verbose helper function to make it work. What am I missing here?

 createRobots: -> # process robot commands
        createXcoord = missionData.xCoord
        createYcoord = missionData.yCoord
        createOrient = missionData.orientation
        createInstru = missionData.robotInstructions
        for command in createOrient
          robot = 'robot' + (_i + 1)
          name = robot
          robot = new Robot \ # create named Robot 
            name
          , createXcoord[_i] 
          , createYcoord[_i] 
          , createOrient[_i] 
          , createInstru[_i]
          console.log(robot)

I think what is happening is that the variable "robot = 'string' is written over when the robot = new Robot is declared.

The outcome I am hoping for is string1 = new Robot, "string2 = new Robot". Does that make sense? jsfiddle.net/7EN5y/1

1 Answer 1

2

You need to add them to a context. If you want them to be global, create a variable like this:

# Either the browser root, or the CommonJS (e.g. Node) module root
root = window or exports

If you want an object that holds robots, add such an object to the root.

root.robots = []

Then when creating robots, add them to such an object.

robot = 'robot' + (_i + 1)
name = robot
robot = new Robot # ...
root[name] = robot # or robots[name] = robot

You may then use code like robot1.move(), or robots.robot1.move() (depending on if you attached them to the root or not).

8
  • But then you'd need to know the names elsewhere in order to access them as root[name]. I think something along the lines of root.robots = { } is the only sane way to proceed. May 19, 2013 at 22:23
  • Sorry I marked your answer as accepted and then not. It helps in so much as it fixes a scope issue I had, but does not actually seem to address the question I asked. May 19, 2013 at 23:21
  • I think what is happening is that the variable "robot = 'string' is written over when the new Robot is declared. The outcome I am hoping for is string = new Robot. Does that make sense? jsfiddle.net/7EN5y/1 May 19, 2013 at 23:23
  • I guess I don't really understand what you're trying to accomplish. What is the purpose of the line should = 'shouldnt'?
    – Brigand
    May 20, 2013 at 4:49
  • @happilyUnStuck, this is my best attempt at understanding what you want.
    – Brigand
    May 20, 2013 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.