Connecting Tech Pros Worldwide Help | Site Map

Problem using object methods as event handlers

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 4th, 2005, 12:05 AM
D. Patterson
Guest
 
Posts: n/a
Default Problem using object methods as event handlers

Hello all.

I've a bit of a problem. I would like to use a method in an object as an
event handler. Unfortunately, when the method is called the "this" object
references the event trigger rather than my object.

For example, with the following code, an alert will be shown when the page
loads
where the value of my_name is "My Object" and nodeName is 'undefined".
Dismissing
that alert and then clicking anywhere on the page will present another alert
where the
value of my_name is "undefined" and the value of nodeName is "BODY".

<html>
<head>
<title>Event Example</title>
<script type="text/javascript" language="JavaScript">
function x( n )
{
this.my_name = n;
}
x.prototype.toString = function( level ){ return "ABC"; }
x.prototype.event_handler = function( e )
{
var evt = ( e ? e : ( window.event ? window.event : null ));
if( !evt ) return;
alert( "x.event_handler()\n" +
" my_name:\t" + this.my_name + "\n" +
" nodeName:\t" + this.nodeName + "\n" +
" event:\t\t" + evt.type );
}

function body_onload()
{
my_obj = new x( "My Object" );
my_obj.event_handler( { type:'Dummy' } );
document.body.onclick = my_obj.event_handler;
}
</script>
</head>
<body onload="body_onload();">
<input id="Button1" name="Button1" type="button" value="Button 1">
</body>
</html>

Any suggestions as to how to get around this?

Thanks in advance.,

Dave




  #2  
Old November 4th, 2005, 01:47 AM
Richard Cornford
Guest
 
Posts: n/a
Default Re: Problem using object methods as event handlers

D. Patterson wrote:[color=blue]
> I've a bit of a problem. I would like to use a method in an object as
> an event handler.[/color]

Not at all an unusual desire.
[color=blue]
> Unfortunately, when the method is called the "this"
> object references the event trigger rather than my object.[/color]
<snip>

There are a number of techniques that handle this problem. As you are
assigning the handlers as a function reference the most appropriate
method is described here:-

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >

Richard.


  #3  
Old November 4th, 2005, 02:15 PM
D. Patterson
Guest
 
Posts: n/a
Default Re: Problem using object methods as event handlers

Wow. Great article.
Now I go play for a while.
I'll post the results....later.

Thanks
Dave

"Richard Cornford" <Richard@litotes.demon.co.uk> wrote in message
news:dkeh53$kbv$1$8300dec7@news.demon.co.uk...[color=blue]
> D. Patterson wrote:[color=green]
> > I've a bit of a problem. I would like to use a method in an object as
> > an event handler.[/color]
>
> Not at all an unusual desire.
>[color=green]
> > Unfortunately, when the method is called the "this"
> > object references the event trigger rather than my object.[/color]
> <snip>
>
> There are a number of techniques that handle this problem. As you are
> assigning the handlers as a function reference the most appropriate
> method is described here:-
>
> <URL: http://www.jibbering.com/faq/faq_notes/closures.html >
>
> Richard.
>
>[/color]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.