bind operator (was: arrow function syntax simplified)

Russell Leggett russell.leggett at gmail.com
Wed Mar 28 14:25:25 PDT 2012


On Wed, Mar 28, 2012 at 5:19 PM, Erik Arvidsson <erik.arvidsson at gmail.com>wrote:

> I find this proposal backwards. It requires an expeession as the right
> hand side. Alex proposed something similar a long time ago. He
> suggested using '!' instead of '.'. This is important because the
> common use case we want to solve is to make 'obj.foo.bind(foo)' be
> written as 'obj!foo'. With your proposal I have to do let f = obj.foo;
> obj::f which is even longer than the original.
>

Yes, this was the way that my proposal worked as I mentioned it in the
other thread (sorry, this is getting confusing now)
=== from my orginal post in the short function thread ===
 //here the # acts as both a . and bind in one
needsCallback(foo#bar)
needsCallback(this#bar)

//in this case, it obviously doesn't access a property, it just does a bind
needsCallback(this#function(x,y){
if(x){
this.doX(x);
else{
this.doY(y);
}
});
//or with the new syntax
needsCallback(this#(x,y)->{
...
});
=========

I used # instead of !, but effectively the same idea.



>
> Another important, but orthogonal part of his proposal was to have
> obj!meth === obj!meth
>
> expression!propertyName
>
> // desugars to
>
> // WeakMap to cache the lookup.
> let objectMap = new WeakMap;
>
> do {
>  let object = expression;
>  let map;
>  if (!(map = objectMap.get(object)) {
>    map = new Map;
>    objecMap.set(object, map);
>  }
>  let result;
>  if ((result = map.get("propertyName")))
>    return result;
>  result = object.propertyName.bind(object);
>  map.set("propertyName", result);
>  result;
> }
>
> One option would be to use the function instead of the property name
> as the key in the inner map.
>

Yes, this was something I mentioned to John Tamplin regarding his concern
above about unregistering a handler.

I'm writing up a more detailed proposal that I will post soon.

- Russ



>
> On Wed, Mar 28, 2012 at 10:37, David Herman <dherman at mozilla.com> wrote:
> > On Mar 27, 2012, at 9:14 PM, Russell Leggett wrote:
> >
> >> I'm sure this is a bit of a tangent, but the other major related case
> is passing a "method" as an argument but needing to retain the correct
> "this". Obviously, that is what bind was meant for, but that is
> inconvenient when passing methods for the same reason it would be
> inconvenient for anonymous functions.
> >>
> >> What if we had a shorthand for bind that worked in both of these
> cases...
> >
> > I have a strawman on this topic:
> >
> >    http://wiki.ecmascript.org/doku.php?id=strawman:bind_operator
> >
> > I agree it's a real need. Just like ... will provide convenient,
> high-fidelity (e.g., insensitive to Function.prototype monkey-patching)
> syntax for .apply, we should also have an operator that provides
> convenient, high-fidelity syntax for .call and .bind. I believe a single
> operator can provide both.
> >
> > Dave
> >
> > _______________________________________________
> > es-discuss mailing list
> > es-discuss at mozilla.org
> > https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> --
> erik
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120328/a27daa4d/attachment-0001.html>


More information about the es-discuss mailing list