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

Community
  • 1
  • 1
happilyUnStuck
  • 372
  • 3
  • 18

1 Answers1

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

Brigand
  • 84,529
  • 20
  • 165
  • 173
  • 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. – mu is too short May 19 '13 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. – happilyUnStuck May 19 '13 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? http://jsfiddle.net/7EN5y/1/ – happilyUnStuck May 19 '13 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 '13 at 04:49
  • @happilyUnStuck, this is my [best attempt](http://jsfiddle.net/7EN5y/2/) at understanding what you want. – Brigand May 20 '13 at 04:56
  • @FakeRainBrigand thank you for trying. What I am trying to achieve is to create a new class name from a string. `shouldwork = new Class` works if I want a class instance called `thisdoesnotwork` but I only have that value as a string "thisdoesnotwork"` and I assign it to variable `shouldwork = "thisdoesnotwork"` and then declare the class as `shouldwork = new Class` i get a class named `shouldwork` and not `thisdoesnotwork`. I know my naming was unclear, it was 4am over here! =) – happilyUnStuck May 20 '13 at 06:07
  • so what I am asking is whether it is possible in javascript to acheive something that approximates `"string" = new class` `string.works()` – happilyUnStuck May 20 '13 at 06:15
  • `something = new Class` is equivalent to `window.something = new Class` and `window["something"] = new Class`. So if you have a variable called `MyVar` which holds the string `"something"`, do `window[MyVar] = new Class`. – Brigand May 20 '13 at 06:19