[Project_owners] [JS] addEventListener function parameter

Aaron Boodman zboogs at gmail.com
Tue May 30 21:28:59 EDT 2006


Sorry for the lingo. partial() and bind() are functional programming
things that some JS programmers have borrowed. MochiKit has the best
implementation I'm aware of which is open source:

http://www.mochikit.com/doc/html/MochiKit/Base.html#fn-bind

Unfortunately, their implementation is heavily tied into MochiKit
itself, which is designed for DHTML, not extensions. I don't know of a
good, optimized, free standing implementation which is OSS. Where I
work, we have one, and I'm looking into making it OSS, but it isn't
yet. (Sorry).

Anyway, what it does. If you have a function, f, and some arguments
a1, a2, a3. partial(f, a1, a2, a3) will create a new function f'
which, when called, will call f(a1, a2, a3). Additionally, any
arguments passed to f' will be passed to f after a3. So f'(a4) ==
f(a1, a2, a3).

bind() is the same, except for that it also takes an object for the
|this| value of f. So bind(f, o, a1, a2, a3) == f.apply(o, [a1, a2,
a3]);

- a

On 5/30/06, Eric H. Jung <eric.jung at yahoo.com> wrote:
> For those less knowledgable about JS, can you please explain what you
> mean by partial() and bind()?
>
> Thank you,
> Eric
>
> --- Aaron Boodman <zboogs at gmail.com> wrote:
>
> > Jeez, I can't type today. That should have been:
> >
> > for (var i = 0; i < 5; i++) {
> >   partial(handleClick, i);
> > }
> >
> > or, you can do it inline with an anon function like:
> >
> > partial(function(arg) {
> >   // do stuff w/ arg
> > }, i);
> >
> > I think the main argument against this is that it can cause memory
> > leaks. This is a fair complaint. I prefer to take the chance and
> > later
> > debug the leaks, since I greatly prefer this form and since the leaks
> > are mozilla bugs that should hopefully someday go away.
> >
> > But like I said, I'm interested in counter arguments.
> >
> > - a
> >
> > On 5/30/06, Aaron Boodman <zboogs at gmail.com> wrote:
> > > On 5/30/06, Nickolay Ponomarev <asqueella at gmail.com> wrote:
> > > > - but you're better off not using this unless you understand how
> > it
> > > > works. (And it's really unnecessary if all you need to pass is a
> > > > simple string).
> > >
> > > eh... I prefer this form. I think tacking attributes onto random
> > > elements to store state is, err, tacky and has more chance to
> > collide
> > > with other people's random additions. Also, with bind() it is
> > really
> > > quite elegant:
> > >
> > > for (var i = 0; i < 5; i++) {
> > >   button.addEventListener("click", bind(i); i++);
> > > }
> > >
> > > Finally, I've read that attributes are quite expensive in mozilla,
> > > since they can be tied to CSS, XBL, etc.
> > >
> > > Why do you think it's better to store something in the DOM rather
> > than
> > > in pure JS?
> > >
> > > - a
> > >
> > _______________________________________________
> > Project_owners mailing list
> > Project_owners at mozdev.org
> > http://mozdev.org/mailman/listinfo/project_owners
> >
>
>
> Use Your PC To Find a Cure for Cancerhttp://members.ud.com/services/teams/team.htm?id=68C9E079-8285-495F-8598-D73352CC7075And Join Team Lulu
>


More information about the Project_owners mailing list