Hi everyone.
I'm dealing with some javascript code which uses eval to access
properties of an object.
For instance, I have the following:
var events = {};
events.flatUsers = {};
events.flatUsers.Clone = "I'm the Clone property";
events.flatUsers.Edit = "I'm the Edit property";
events.flatUsers.Delete = "I'm the Delete property";
var key = "flatUsers.Clone";
Right now, given 'events' and the key 'flatUsers.Clone', eval is used to
retrieve events.flatUsers.Clone
window.alert( eval("events." + key) );
I'd like to get rid of eval, and since I cannot do just:
events[key]
I wrote the following:
function evaluate() {
var context = this;
for(var i = 0; i < arguments.length; i++) {
context = context[arguments[i]];
}
return context;
}
window.alert(evaluate.apply(events, key.split('.')));
Surely this can be improved, since I'm a newbie in Javascript. Any
suggestions?
Regards,
Ignacio Burgueño