Possible Duplicate:
Create instance of “Class” Using “Reflection” in JavaScript
Java has Class.forName("XYZClass")
to load class at run time. Is there any alternate for Class.forName("XYZClass")
in JavaScript?
Possible Duplicate:
Create instance of “Class” Using “Reflection” in JavaScript
Java has Class.forName("XYZClass")
to load class at run time. Is there any alternate for Class.forName("XYZClass")
in JavaScript?
Personally I think you are trying to literally translate Java code to JavaScript rather than using idiomatic constructs (well, Class.forName()
isn't idiomatic in Java as well). However, let's give it a try:
function XYZClass() {
this.answer = 42;
}
This is your class. Normally you would create it using:
var xyz = new XYZClass();
With "reflection" in JavaScript this works:
var className = "XYZClass";
var xyz = new window[className]();