Possible Duplicate:
How to convert a string value to a variable in javascript?
In Javascript, I have an object that looks like this:
clock = function(){
this.load = function(){
alert("once");
}
this.once = function(){
alert("load");
}
}
var clock = new clock();
On the page load, I call the load() function within the clock object like so
clock.load();
When the user clicks a link, however, I need to be able to run clock.once(). Calling it like this is fine, but does not fit the dynamic needs of what I'm doing.
Let's say I need to call clock.once() when the user clicks an <a> tag:
$("a").click(function(){
var id = $(this).attr("href").match(/[a-zA-Z]+/g);
[id].once();
}
I figured that would work, where the object that the once() function is being called from is grabbed from the string id.
When running this, however, I get the following error:
Uncaught TypeError: Object clock has no method 'once'
Manually calling once() by using clock.once() works fine.
clock.once()? Why not haveonceaccept the id argument?clock, but when a person clicks a link that has a differenthrefattribute, it needs to run theonce()function within theidobject (that is the same as thehrefattr clicked, they are always the same).