1

实例化的通常情况是new MyClass(arg1).

MyClass当一个变量的值仅在运行时可用时,我如何对这种情况进行编码?

4

2 回答 2

6

All globals are properties of the global object.
You can get object properties by name using indexer notation:

new global[someString]();

In a browser, the global object is window.

于 2013-05-29T19:44:31.853 回答
1

If what you're saying is that you have "MyClass" as a string-variable in JavaScript, you can do it with Eval:

var yourvar = "MyClass";
eval("new " + yourvar + "();");

Note: eval can be dangerous and should be avoided when possible. In this case it would work, but it's not the recommended method. If you can show us your use case, we could maybe give you a better, safer solution.

于 2013-05-29T19:45:27.467 回答