I have something like this...
define(['ClassA', 'ClassB', 'ClassC'],
function(ClassA, ClassB, ClassC)
{
return {
build: function(className) {
var obj;
switch(className)
{
case 'ClassA': obj = new ClassA(); break;
case 'ClassB': obj = new ClassB(); break;
case 'ClassC': obj = new ClassC(); break;
}
return obj;
}
}
}
This seems ok but is there a better way to write it? I tried replacing switch to
return new arguments[className](); // doesn't work
The closest I can get to is to use a map:
var classes = {
ClassA: ClassA,
ClassB: ClassB,
ClassC: ClassC
}
return new classes[className]();
Is there a better way?