I need to create an object from its name in Node.js. How can I do it (without using eval)?
For example, I've tried something like the code bellow, but it failed on `Person is unknown'. I guess it has something with the context (this). I tried the bind/call/apply functions, but with no avail.
module.export = {
class Person = {/*....*/};
var createObject = function(name) {
return Function('return new ' + name + '();')();
};
const p1 = new Person(); // works
const p2 = eval('return new Person();'); // works, but has security issues
const p3 = createObject('Person'): // doesn't work.
}:
I’d appreciate it if you could show me how to write the createObject function right.