Re: Add extra argument to addEventListener with the context

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