C
cainlevy
Something that's been bugging me for a while: I can't figure out how to
register an object method as an event handler, and still use the 'this'
keyword inside the handler to refer to the object. For example:
function myObj ()
{
this.toString = function(){return 'myObj'}; //define string
representation of this object
document.getElementById('test_tag_id').onclick = this.test; //attach
to some clickable element
}
myObj.prototype.test = function()
{
alert('object: ' + this);
}
tmp = new myObj(); //instantiate
tmp.test(); //verify the function works normally ... should report
'object: myObj'
Now, when I click on the object, the alert dialog reports 'object:
[object HTMLTableElement]'. Any ideas?
register an object method as an event handler, and still use the 'this'
keyword inside the handler to refer to the object. For example:
function myObj ()
{
this.toString = function(){return 'myObj'}; //define string
representation of this object
document.getElementById('test_tag_id').onclick = this.test; //attach
to some clickable element
}
myObj.prototype.test = function()
{
alert('object: ' + this);
}
tmp = new myObj(); //instantiate
tmp.test(); //verify the function works normally ... should report
'object: myObj'
Now, when I click on the object, the alert dialog reports 'object:
[object HTMLTableElement]'. Any ideas?