Batakj
Batakj

Reputation: 12743

how to cast string to a function in javascript?

I didn't find the keyword for google...

My Problem is

let's have a json array

{
  "a":"b",
  "c" : "d",
  "api" : 
  {
    "name":"listOfNetwork",
    "param" :['xyz','xyz1']
  } 
}

Now i have a javascript method

function listOfNetwork(param1, param2) //name-of-api, params
{
     // do something.
}

how to invoke this function by retrieving the name and parameters from JSON.

Please help

Upvotes: 2

Views: 77

Answers (1)

Ivan Castellanos
Ivan Castellanos

Reputation: 8243

window[json.api.name].apply(this, json.api.param)

Thats all.

Upvotes: 6

Related Questions