实例化的通常情况是new MyClass(arg1)
.
MyClass
当一个变量的值仅在运行时可用时,我如何对这种情况进行编码?
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
.
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.