从javascript中的字符串名称获取对象类 [英] Get object class from string name in javascript

查看:654
本文介绍了从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]()

这篇关于从javascript中的字符串名称获取对象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆