[Javascript] Comparing objects

Bill Moseley moseley at hank.org
Tue May 16 18:45:53 CDT 2006


On Tue, May 16, 2006 at 10:43:19PM +0200, liorean wrote:
> On 16/05/06, Bill Moseley <moseley at hank.org> wrote:
> >Maybe this is my lack of understanding of "this".
> 
> 
>    elm.onevent=fn;
>    elm['onevent']=fn; // event triggered --> 'elm'
>    // If event triggers, fn will be called with elm as it's this object.
>    // "Why, when there's no 'elm.onclick()' in there?" you might ask.
>    // The answer is: Because that's what Netscape originally did.

That helps.  That info helped me solve the problem.
If I use onmouseover then "this" can be used to modify the className of
the element.  Simple.

> 
>    elm.attachEvent('onevent',fn); // event triggered --> 'global'
>    // Microsoft proprietary event registration, fn is not a method of elm and
>    // attachEvent does not make fn take elm as it's this object, so this will
>    // be the global object.
> 
>    elm.addEventListener('event',fn,false) // event triggered --> impl. dependant
>    // W3C event registration. Does not deal with the this object, so  actually
>    // you cannot rely on it being 'elm' in this case. But you shouldn't have to use
>    // this anyway, since you have 'ev.target' and 'ev.currentTarget' in all
>    // implementations of addEventListener that I know of.

I would prefer this method since I don't have to worry about stomping
on another event handler already sent, but it looks like more work
getting at the actual element I'm interested.

Thanks for the explanation.

-- 
Bill Moseley
moseley at hank.org




More information about the Javascript mailing list