Create instance of “Class” Using “Reflection” in JavaScript

后端 未结 3 655
醉话见心
醉话见心 2021-01-19 05:28

I searched the web for some way to create instance of \"Class\" in \"reflection\" using javaScript but I found nothing.

generally i\'m trying to do someting like jav

3条回答
  •  旧时难觅i
    2021-01-19 05:59

    JavaScript doesn't have classes. 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.

提交回复
热议问题