Possible Duplicate:
How do I make JavaScript Object using a variable String to define the class name?
I want to be able to call the new on things programmatically. So for example, if I have these two classes:
class Bird
constructor: (@name) ->
class Snake
constructor: (@name) ->
I can call s = new Snake("Sam"). Now, what I want to do is
bird_class = "Bird"
b = new bird_class()
and be able to construct a bird object from a string. Is this possible in CoffeeScript or Javascript?
If you kept your classes in an object you could always:
As per your comment, some pattern like this would let you register classes
on the fly. It’s raw, but might give you an idea: