I am writing a class factory that is a function that accepts parameters to define a class and returns a javascript class (function with a 'this' context) that is a named function, like
function ClassName(){
// ...
}
Its all working great except for the naming of the function. There doesn't seem to be a way to dynamically create a named function.
I admit, this is mearly a stylistic problem and does not alter the functionality of the class.. I just really like seeing the class names in the console, as well as the names of all ancestor classes. It really makes debugging a lot easier.
Currently I am using eval()
to accomplish this using code similar to.
function classGen(className, constructorFn, proto){
var NamedClass;
eval('NamedClass = function ' + className + '{ constructorFn.apply(this, arguments) };');
for(var key in proto)
NamedClass.prototype[key] = proto[key];
return NamedClass;
}
etc....
Would this be considered a "safe" use of the eval function?
Why / why not?
---edit---
I dont know why i didn't consider
className = className.replace(/[^a-z0-9]/gi, '');