从javascript中的字符串名称获取对象类

问题描述:

我想从Javascript的名称中获取对象。
我在一个应用程序,需要加载一些不同的上下文,我试图加载不同的类与继承jquery插件。一切都很好,除了当我需要建构一个类,我不能,因为我只有类的名称,而不是直接的对象。

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.

基本上,我想找到像'getClass(String name)'。有人可以帮助我吗?

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

不要使用 eval()

您可以将地图存储在地图中:

You could store your classes in a map:

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

然后查找:

new classes[name]()