I’d like to create some kind of factory to automate the process of creating JS classes. The factory should loop through an object that contains classes names and their respective parameters. The following simplified code sample hopefully demonstrates what I mean:
// function for dependency injection
addNumbers = (numbers) => {
let result = 0;
numbers.forEach((number) => {
result = result + number;
})
console.log(`nThe result is ${result}`);
}
// class without parameters
class Log {
constructor() {
console.log ('My only job is to log this text!n');
}
}
// class with parameters
class Greeting {
constructor(params) {
this.sayHello(params);
}
sayHello(params) {
for (const [key, name] of Object.entries(params)) {
console.log(`Hallo ${name}, nice to meet you!`);
}
}
}
// class with parameters
// and dependency injection
class Addition {
constructor(params) {
this.injectedMethod = params['dependency'];
this.injectedMethod(params['numbers']);
}
}
classParams = {
Log: '',
Greeting: {
name1: 'Alice',
name2: 'Bob'
},
Addition: {
numbers: [1, 2, 3, 4, 5],
dependency: addNumbers
}
}
// Here comes the critical part:
// I'd like to have a loop that replaces the
// following code and iterates through
// classParams, creating the classes with
// their respective parameters
new Log();
new Greeting(classParams['Greeting']);
new Addition(classParams['Addition']);
I tried something like
for (const [object, params] of Object.entries(classParams)) {
new [object](params);
}
.. but that won’t work because inside the loop I don’t get the class as an object but only as a string. I tried some things I found online, but the best I could achive was having no errors – but having no working classes either.
What am I getting wrong?
2
Answers
You can put all the classes into an object and use bracket notation to access classes by name using it.
Object
s only use strings for keysUnfortunately, JavaScript doesn’t have support for values as keys with the
Object
class. There are, however, many options available to store classes with an argument or arguments to call them with. Below is a non-exhaustive list of such:Using an
Object
andeval
to get the classThis is a safe usage of
eval
, so no need to worry about that.Using an
Array
or aMap
to store the class itselfUsing one
Object
to store the classes and another to store the argumentsCredit goes to @Unmitigated for this technique.
Sidenote
If your constructor has multiple parameters or no parameters, I would recommend using an array to hold the arguments and using the spread syntax (i.e.
...
) to pass the arguments to the class.Also if you need to create multiple of the same class with different arguments, I would recommend the
Array
technique as shown above.