Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  26] [ 4]  / answers: 1 / hits: 17755  / 13 Years ago, wed, april 13, 2011, 12:00:00

I would like to get an object from its name in Javascript.
I'm working on an application which will need to load up some different context, I'm trying so to load different classes with the inherit jquery plugin. Everything works just fine, excepts that, when I need to instanciate a class I can't because I've only the name of the class and not the object directly.



Basically, I would like to find something like 'getClass(String name)'. Does anyone could help me ?


More From » string

 Answers
6

Don't use eval().



You could store your classes in a map:



var classes = {
A: <object here>,
B: <object here>,
...
};


and then just look them up:



new classes[name]()

[#92764] Tuesday, April 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;