Re: .addEventListener() not finding the right object method
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: .addEventListener() not finding the right object method

From: Michael Winter <m.winter@blueyonder.co.uk>
Date: Fri Aug 26 2005 - 23:18:09 CEST

On 26/08/2005 19:58, Liming wrote:

[snip]

> You have the var instance = this; defined in the class (private
> variable).. and such it's accessible to the inner functions of the
> "constructor" class ONLY.
>
> what if you are calling from a method of .prototype.mehtod()? In this
> case, i wont' have access to the private instance variable "instance".
> What's the best way to do this? Move this prototype method into the
> class "consturctor" as an inner function?

[snip]

You could do that, though it's not the only solution. An alternative is
to create a property on the element that references your object. When
your listeners are executed, the this operator will reference the element:

   function MyObject() {
     /* ... */
   }
   MyObject.prototype.listener = function() {
     /* Use 'this.object' to get reference to object instance. */
   };

   var o = new MyObject();

   myElement.object = o;
   myElement.addEventListener('...', o.listener, false);

Hope that helps,
Mike

-- 
Michael Winter
Prefix subject with [News] before replying by e-mail.
Received on Tue Oct 18 03:13:17 2005