如何使用变量String创建JavaScript对象来定义类名? [英] How do I make JavaScript Object using a variable String to define the class name?
本文介绍了如何使用变量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();
推荐答案
如果你做了这样的事情,它会起作用吗? / p>
Would it work if you did something like this:
var myObject = window[classNameString];
..?
这篇关于如何使用变量String创建JavaScript对象来定义类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文