Create class constructor using eval: getting ReferenceError

36 Views Asked by At

I'm trying to create class constructor dynamically and came acrosss the following solution:

https://stackoverflow.com/a/40069309/1713920

//TeamMemberProfile.js
export default class TeamMemberProfile{
}

//helpers.js
function createClassByName(name,...a) {
    var c = eval(name);
    return new c(...a);
}

But when I try to use it:

//somewhere in project
let obj = createClassByName("TeamMemberProfile")

It throws ReferenceError: TeamMemberProfile is not defined which isn't a surprise.

However, what should I do in order for eval to understand that code?

DISCLAIMER: whether it's a good or bad idea to use eval is outside of the scope of this question.

0

There are 0 best solutions below