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

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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.
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
I guess I don't really understand what you're trying to accomplish. What is the purpose of the line should = 'shouldnt'?
@happilyUnStuck, this is my best attempt at understanding what you want.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.