I have 2 Class defined somewhere else, all constructors need 3 parameters (a, b, c), but a, b(object), c(object) is in an Array
var paramArr = [a, b, c];
I would like to call these 2 Class via a parameter according to a dictionary object, like:
var dict = {'a': 'A', 'b': 'B'};
then,
callClass(x){ var className = dict[x];//string new className(); }
So, first I tried to do is something like eval so that I could call the Class with it string name.
callClass(x){ var className = dict[x];//string var classFn = new Function(this, 'new ' + className + '()'); classFn(); }
still works fine. at last I should add the args by Fn.prototype.apply(), then I had to say all messed up:
callClass(x){ var className = dict[x];//string paramArr.unshift(null); var functionBodyStr = 'return new (Function.prototype.bind.apply(' + className + ', '+ paramArr +'))'; var classFn = new Function(this, functionBodyStr); classFn(); }
the classFn in the console will be:
function(this){
return new (Function.prototype.bind.apply(Classname, , a ,[object Object],[object Object]))
}
it is something I expected but not all:
first, the 'null' value in the array is converted to disappeared, which leads to 'unexpected token ,'
second, all the other objects are converted to '[object Object]', which leads to 'unexpected identifier'
So, how could I apply a array param with null value? or this is a wrong way to do this?