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(){retu rn 'myObj'}; //define string
representation of this object
document.getEle mentById('test_ tag_id').onclic k = 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 HTMLTableElemen t]'. 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(){retu rn 'myObj'}; //define string
representation of this object
document.getEle mentById('test_ tag_id').onclic k = 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 HTMLTableElemen t]'. Any ideas?
Comment