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

发布于 2024-10-31 16:03:32 字数 187 浏览 1 评论 0原文

我想从 Javascript 中的名称获取一个对象。 我正在开发一个需要加载一些不同上下文的应用程序,我正在尝试使用“继承”jquery 插件加载不同的类。一切都工作得很好,除了,当我需要实例化一个类时,我不能,因为我只有类的名称,而不是直接的对象。

基本上,我想找到类似“getClass(String name)”的东西。有人可以帮助我吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兰花执着 2024-11-07 16:03:32

不要使用eval()

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

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

然后查找它们:

new classes[name]()

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]()
灵芸 2024-11-07 16:03:32

JavaScript:基于字符串调用函数

 function foo() { }
 this["foo"]();

JavaScript: Call Function based on String:

 function foo() { }
 this["foo"]();
葮薆情 2024-11-07 16:03:32

您可以完美地使用 eval() 而不会产生安全风险:

var _cls_ = {}; // serves as a cache, speed up later lookups
function getClass(name){
  if (!_cls_[name]) {
    // cache is not ready, fill it up
    if (name.match(/^[a-zA-Z0-9_]+$/)) {
      // proceed only if the name is a single word string
      _cls_[name] = eval(name);
    } else {
      // arbitrary code is detected 
      throw new Error("Who let the dogs out?");
    }
  }
  return _cls_[name];
}

// Usage
var x = new getClass('Hello')() // throws exception if no 'Hello' class can be found

优点:您不必手动管理地图对象。

缺点:无。有了正确的正则表达式,任何人都无法运行任意代码。

You can perfectly use eval() without a security risk:

var _cls_ = {}; // serves as a cache, speed up later lookups
function getClass(name){
  if (!_cls_[name]) {
    // cache is not ready, fill it up
    if (name.match(/^[a-zA-Z0-9_]+$/)) {
      // proceed only if the name is a single word string
      _cls_[name] = eval(name);
    } else {
      // arbitrary code is detected 
      throw new Error("Who let the dogs out?");
    }
  }
  return _cls_[name];
}

// Usage
var x = new getClass('Hello')() // throws exception if no 'Hello' class can be found

Pros: You don't have to manually manage a map object.

Cons: None. With a proper regex, no one can run arbitrary code.

ゝ杯具 2024-11-07 16:03:32

你是这个意思吗?

function Person(name){
    this.name = name;
}

function getClass(str_name, args){
    return new (window[str_name])(args);
}

var wong2 = getClass("Person", "wong2");

alert(wong2.name);   // wong2

Do you mean this?

function Person(name){
    this.name = name;
}

function getClass(str_name, args){
    return new (window[str_name])(args);
}

var wong2 = getClass("Person", "wong2");

alert(wong2.name);   // wong2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文