I am trying to create a function that deserialises a JSON object and creates some functions, but I want to be able to specify a variable set of arguments.
For example, in JSON I want to specify something like:
{
"handler" :
{
"args" : ["evt","value"],
"content" : "console.log(value);"
}
}
And when I parse the object, I will do something like:
var myFunction = new Function(handler.args,handler.content);
But the challenge is that each argument is supposed to be a n strings followed by the content of the function as the last argument. Is there any easy way of specifying n number of arguments in a new Function()?
According to MDN
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
Simply passing your
handler.argsarray as the first argument to thenew Functionconstructor should work exactly as you want it toInternally,
new Functioncasts every argument to a string if it is not already one. So conceivably something like this would also workNot that I’m suggesting you do anything silly like that.
This works in every browser I’ve tried including IE