0

For example I have a class called Daah, I'd like to instantiate it using a dynamic string variable:

class Daah{

}

var classname = "Daah";
var obj = new window[classname];

I thought this would work but returns undefined. Is there a way to dynamically create constructors?

K3NN3TH
  • 1,458
  • 2
  • 19
  • 31
  • You might get a fair idea about the dynamic object construction from here - https://stackoverflow.com/a/3871769/7994074 – ParthS007 Jun 09 '19 at 00:21

1 Answers1

0

You have to attach the class to the window:

class Daah{

}

window.Daah = Daah;
var classname = "Daah";
var obj = new window[classname]; // class Daah {  }
Daniel Bank
  • 3,581
  • 3
  • 39
  • 50