JavaScript Function arguments
To solve the technically issue: You can use apply
[docs].
handler.args.push(handler.content);var myFunction = Function.apply(null, handler.args);
However the question is why you are doing something like this? What is the context? Spontaneously I would say you should consider another solution for whatever problem you are trying to solve ;)
According to MDN
Parameters
arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "x", "theValue", or "a,b".
So the arguments list can either be one or more strings seperated by commas, or just one string with each identifier in it seperated by commas.
Also since
['evt', 'value'].toString() == 'evt,value'
Simply passing your handler.args
array as the first argument to the new Function
constructor should work exactly as you want it to
new Function(handler.args, handler.content);
Internally, new Function
casts every argument to a string if it is not already one. So conceivably something like this would also work
new Function({ toString: function() { return 'a,b,c' } }, 'return a+b+c');
Not that I'm suggesting you do anything silly like that.
This works in every browser I've tried including IE
I think the simplest route would be to combine the 2 properties. Then use apply
to construct the function.
var x = { "handler" : { "constructorArgs" : [ "evt", "value", "alert(value);" ] }};var f = Function.apply(undefined, x.handler.constructorArgs);f(1, 2);
To keep it similar you can use Array.prototype.concat
.
var x = { "handler" : { args: [ "evt", "value" ], content : "alert(value);" }};var f = Function.apply(undefined, x.handler.args.concat(x.handler.content));f(1, 2);