0

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?

13
  • 1
    You can put them in an object cls = { Foo: class Foo {}, Bar: class Bar {} } and do property access myFoo = new cls[classToLoad]() Commented May 22, 2018 at 21:31
  • Is it possible to do that if I don't know what classes may be available at design time? This is meant to be a pluginable system. Commented May 22, 2018 at 21:32
  • Plug them in by extending the main object. Then make sure that property exists before calling it Commented May 22, 2018 at 21:33
  • Imagine var x=1, y=2; var iNeed="y"; console.log(iNeed); you want it to print "2"? Commented May 22, 2018 at 21:35
  • 2
    It seems like the sensible thing to do would be to require plugins to register themselves before you can use them. The registration would associate a string to a class. This would also allow you to provide sensible errors if a user tried to use a non-existent plugin. Commented May 22, 2018 at 21:46

2 Answers 2

3

There should be a container to identify classes by their names. Notice that there may be more than one class of same name, and function/class names are minified in client-side scripts.

class Foo { }
class Bar { }

const container = { Foo, Bar };

let classToLoad = "Foo";

let myFoo = new container[classToLoad]();
Sign up to request clarification or add additional context in comments.

Comments

0

This could help:

var Foo = class Foo { }
var Bar = class Bar { }

var classToLoad = "Foo";

var myFoo = new window[classToLoad]();   // I want this to be equivalent to new Foo;

var classToLoad = "Bar";
var myBar = new window[classToLoad]();   // I want this to be equivalent to new Bar;

10 Comments

You need to have Foo = class Foo { } without "var".
This may work, but I'm not sure it's really achieving what I'm trying to do, which is a create a node.js application to which users can install plugins.
@IvanKuckir If this code is not inside a function, the variables will be global.
@DavidFindlay Put the name-to-class mapping in an object, and provide a plug-in registration function that adds to the object.
@estus const, let and class don't add a property to the global object even when they are evaluated in global scope.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.