开发者

How to declare and use the name of a function from a json object?

开发者 https://www.devze.com 2022-12-27 07:54 出处:网络
I have a json object collection of geo locations that I build in the server. Each of those objects has two properties: \"marker\" and \"onClick\".

I have a json object collection of geo locations that I build in the server. Each of those objects has two properties: "marker" and "onClick".

Marker is for storing a Google Maps marker object and the onClick stores the name of the function to be called when that marker is clicked on the map.

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

M开发者_开发百科y problem is that when I bind the marker with the onClick property, the function won't be found in the DOM and get an error.

Is there a way to declare a property in a json object for using it on an event binding?

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

Thanks!


If you have a function declared in the global object, e.g.

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

Then you can get the reference to it via subscript notation:

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

If the function is part of some other object, just use the name as a subscript on that object:

var func = some_obj[funcname];
0

精彩评论

暂无评论...
验证码 换一张
取 消