0

I have a string in JSON format that tells me information about what function to call and it's parameters and I'm trying to figure out how to send those parameters to the function.

var obj = JSON.parse("{'jFunc':'UpdateStat', 'args':['health','100']}")

var fn = window[obj.jFunc];
if(typeof fn == 'function'){
  fn();  // how do I pass the args correctly here, in order
}

function UpdateStat(statName, statValue){
}

Note that there could be any number of args. In this example I know I could pass args[0] and args[1] but this is just an example to a generic process where we don't know the number of args that may exist.

I also don't want to pass the args array and have the function handle breaking it out. I'd prefer the final function being called be as normal as possible so as to not worry about those details.

user441521
  • 6,942
  • 23
  • 88
  • 160
  • You already have the array as `obj.args`, you should be able to use that as described in the linked question. – James Thorpe Dec 30 '15 at 15:06
  • 1
    FYI: That is not valid JSON in your example and it is missing the closing `}` – epascarello Dec 30 '15 at 15:06
  • Applying the code from the dupe to your example: `fn.apply(this, obj.args)` – epascarello Dec 30 '15 at 15:07
  • @epascarello If we're being really picky, the `"` and `'` should all probably be reversed too since the key names are supposed to be surrounded with `"` :) – James Thorpe Dec 30 '15 at 15:09
  • @JamesThorpe Crap, I did not see that, that would not parse. lol Coffee has yet to kick in. – epascarello Dec 30 '15 at 15:10
  • The string will be coming from C++ ultimately so that's why I went that way. In regards to the ' vs " – user441521 Dec 30 '15 at 15:10
  • @user441521 single quotes with JSON is not valid. All of the properties and strings need to be quoted with double. – epascarello Dec 30 '15 at 15:11
  • Indeed - while the duplicate should answer your question, the code as posted won't actually work - the `JSON.parse` will fail with `Uncaught SyntaxError: Unexpected token '`. Of course, hopefully you're using a decent JSON encoding library in C++, so it will generate perfectly valid JSON in the actual production environment :) – James Thorpe Dec 30 '15 at 15:12
  • hmm, this string is not originated from javascript so I suppose I have to escape the hell out of it to get it formatted correctly from C++ huh. – user441521 Dec 30 '15 at 15:15
  • That really depends on exactly what's happening, but if you're using C++ to output HTML/JavaScript, I would expect you to have some sort of `JavaScriptEncode` and `HTMLEncode` functions available, which you just put the string you wish to output through at the point of rendering the string into javascript. Escaping strings as they move from one environment to another isn't a complex topic, just one that requires attention to detail at the right places. – James Thorpe Dec 30 '15 at 15:17
  • To make matters worse it's really ultimately coming from Lua, going to C++, and then to Javascript :). I'll play around with it. – user441521 Dec 30 '15 at 15:18

1 Answers1

2

You can use Function.prototype.apply():

window[obj.jFunc].apply(this, obj.args);

see docs here