如何使用变量 String 定义类名来创建 JavaScript 对象? [英] How do I make JavaScript Object using a variable String to define the class name?

查看:32
本文介绍了如何使用变量 String 定义类名来创建 JavaScript 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的——这是伪代码,不起作用.有谁知道如何真正做到这一点:

Here's what I'm trying to do -- this is pseudo code and doesn't work. Does anyone know how to accomplish this for real:

// Define the class
MyClass = Class.extend({});

// Store the class name in a string
var classNameString = 'MyClass';

// Instantiate the object using the class name string
var myObject = new classNameString();

推荐答案

如果你做这样的事情,它会起作用吗:

Would it work if you did something like this:

var myObject = window[classNameString];

..?

这篇关于如何使用变量 String 定义类名来创建 JavaScript 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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