Dynamically generate new named class from string with coffeescript -
i have looked around , have found number of questions approach using-a-string-to-define-the-class-name , dynamic-class-generation-in-coffeescript> neither of them address problem, wondering whether making fundamental mistake in approach problem.
in loop below looping through data parsed json. each set of data want extend class robot string = new robot string string.
currently code not produce errors, , creates new robots since name string, trying access them robot1.move() or robot2.dosomeotherclassything() not work , tells me undefined.
this seems should not require verbose helper function make work. missing here?
createrobots: -> # process robot commands createxcoord = missiondata.xcoord createycoord = missiondata.ycoord createorient = missiondata.orientation createinstru = missiondata.robotinstructions 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 happening variable "robot = 'string' written on when robot = new robot declared.
the outcome hoping string1 = new robot, "string2 = new robot". make sense? jsfiddle.net/7en5y/1
you need add them context. if want them global, create variable this:
# either browser root, or commonjs (e.g. node) module root root = window or exports if want object holds robots, add such object root.
root.robots = [] then when creating robots, add them such object.
robot = 'robot' + (_i + 1) name = robot robot = new robot # ... root[name] = robot # or robots[name] = robot you may use code robot1.move(), or robots.robot1.move() (depending on if attached them root or not).
Comments
Post a Comment