I'm trying to implement polymorphism in Javascript/node.js. I'm trying to do something, but I'm not sure what it's called so I'm not even sure what I'm searching for.
I have a number of different ES6 Classes that are interchangeable and have the same interface. I'm trying to make a system that is extensible and pluginable, so the classes available won't be known at design time.
I want to have a variable which defines the name of the class that I want to create an instance of. So effectively I want to have the following:
class Foo { }
class Bar { }
var classToLoad = "Foo";
var myFoo = new classToLoad; // I want this to be equivalent to new Foo;
var classToLoad = "Bar";
var myBar = new classToLoad; // I want this to be equivalent to new Bar;
Can anyone tell me what the name for this is and how to properly do it?
cls = { Foo: class Foo {}, Bar: class Bar {} }and do property accessmyFoo = new cls[classToLoad]()var x=1, y=2; var iNeed="y"; console.log(iNeed);you want it to print "2"?