JavaScript doesn't have classes.(JavaScript没有类。)
But if by "class" you mean you have a constructor function:(但是,如果使用“类”,则表示您具有构造函数:)
function MyClassName() {
// do constructor things here
}
But the name of that function is in a variable:(但是该函数的名称在变量中:)
var someclass = "MyClassName";
Then you can instantiate an instance like this:(然后,您可以像这样实例化一个实例:)
var obj = new window[someclass]();
The above only works if MyClassName
is in the global scope.(仅当MyClassName
在全局范围内时,以上方法才有效。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…