How can I instantiate a class if all I know is its name, given the following restrictions?
- ES6
- The class is defined by a third-party. I have no way of knowing about the class ahead of time.
All of the answers I've seen on Stackoverflow assume that I define the class being instantiated, and as such I can create a mapping between class names and their constructions ahead of time. Example: https://stackoverflow.com/a/31790015/14731
Seeing as this cannot be done for 3rd-party classes, what can I do?
Is eval() the only way?
What I am trying to do
Users are expected to pass in a class name and I am supposed to instantiate the class, assuming the existence of a constructor that takes exactly one String
argument. More specifically, I am allowing users to override the type of exception my library will throw on error.