W3C home > Mailing lists > Public > www-dom@w3.org > April to June 2012

Re: Add extra argument to addEventListener with the context

From: Glenn Maynard <glenn@zewt.org>
Date: Fri, 15 Jun 2012 16:15:54 -0500
Message-ID: <CABirCh9xREgwQRGn9ef7z=dg5CKm2JpsTnoRsj6t9oY3xpgCRg@mail.gmail.com>
To: Elliott Sprehn <esprehn@gmail.com>
Cc: Boris Zbarsky <bzbarsky@mit.edu>, www-dom@w3.org
On Fri, Jun 15, 2012 at 4:03 PM, Elliott Sprehn <esprehn@gmail.com> wrote:

> It's not that simple, because methods are likely on the prototype object
> defined in advance so there's no chance to call bind(this) before it's
> marked as not-writable.
> This can cause other badness like breaking argument introspection for
> frameworks since the method no longer returns it's source when you do
> instance.method.toString()

You're asking to bloat one API (DOM events) to work around limitations of
other APIs, but it wouldn't stop here; JavaScript uses callbacks
extensively, and the logical conclusion of this--adding this to every
callback API--would be a mess.  function.bind is the standard way of
handling this; you should look to fixing the underlying problems, instead
of trying to bypass it.  (For example, it should probably be possible to
retrieve the wrapped function as an attribute of the bind() function, as
you can with Python's functools.partial.)

Or is your issue that you want to take a class you don't control and use
>> one of its methods as the listener?
> Yes, this may also be the case.

That's trivial; just create a binding of the function yourself.  You don't
have to store it in the external class.

Glenn Maynard
Received on Friday, 15 June 2012 21:16:23 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 22:37:00 UTC