How to declare and use function name from json object?

I have a collection of json geolocation objects that I am creating on the server. Each of these objects has two properties: "marker" and "onClick".

The marker is for storing the Google Maps marker object, and onClick stores the name of the function that will be called when this marker is clicked on the map.

When I push the location objects into an array using client side javascript, I create markers and assign them to each location object in the array.

My problem is when I bind the marker to the onClick property the function will not be found in the DOM and no error will be received.

Is there a way to declare a property in a json object to use it when binding an event?

Hope I could explain it clearly, if not please let me know.

Thanks!

+2


a source to share


1 answer


If you have a function declared in a global object like

function foobar() { alert('foobar') }

      

Then you can get a link to it using index notation:



var funcname = 'foobar';
var func = window[funcname];
func();   // alerts

      

If the function is part of some other object, just use that name as an index for that object:

var func = some_obj[funcname];

      

+4


a source







All Articles