I'm building classes dynamically based on descriptive data pulled from a database. For instance, if the data reads as this:
ClassName = ExampleParent
Method1.name = "greet"
Method1.code = "console.log(\"Parent hello\");"
and building it like this works just fine:
classList[ClassName] = function(){}
classList[ClassName].prototype[Method1.name] = Function(Method1.code);
var Bob = new classList["ExampleParent"]();
Bob.greet();
But I'm stumped on how to dynamically create inheritance:
ClassName = ExampleChild
ClassExtends = ExampleParent
Method1.name = "greet"
Method1.code = "super.greet(); console.log(\"Child hello\");"
I don't see how I could use ExampleChild.prototype to point to both ExampleParent AND contain ExampleChild's custom methods, and even when I tried, it said that super was unknown. I don't need to support anything fancy (private, static, etc)... I just need this and super to work. Hints?
Unfortunately, you can't without using
eval(or effectively usingevalby havingFunctionreturn a function that does the work), but that's still in line with what you're doing asFunctionallows arbitrary code execution just likeevaldoes. Still, it's ugly.It's important that you absolutely trust the source of the data. Because
eval(andFunction) allow executing arbitrary code, you must not (for instance) allow User A to write the code that will be stored in the database for User B to retrieve and use without absolutely trusting User A. It puts User B at risk, potentially exposing them to malicious code from User A.With that caveat in place, you can do it with
eval, e.g.:You may need an object to store the classes on by their runtime names, since that's what you'll need for the
extendsclause.Example: