Scoped binding of a method to an object

Russell Leggett russell.leggett at gmail.com
Tue Oct 15 14:00:13 PDT 2013


On Tue, Oct 15, 2013 at 4:28 PM, Benjamin (Inglor) Gruenbaum <
inglor at gmail.com> wrote:

> Wait, I think maybe I did not understand what you meant before.
>
> Are we talking about using `::` for infixing the first parameter of the
> function? As in `func(a,b,c)` being the same as `a::func(b,c)` ?
>

Not exactly. See the proposal
http://wiki.ecmascript.org/doku.php?id=strawman:bind_operator
"Essentially, e1::e2 is equivalent to do { let obj=e1; e2.bind(obj) }"

Or, in the case of an immediate call, it skips the bind and just does a
Function.prototype.call. In other words,

obj::fun(a,b) is not the same as fun(obj,a,b). Its fun.call(obj,a,b).

The important distinction is that the function being called is expecting a
|this| as opposed to an extra first argument. This is reason I called it
"underscore2" - Brendan picked up on that calling it OO underscore.
Functional purists can argue with the approach, but it would let you write
functions exactly as though they were extension methods - using |this|
where appropriate.

The following is a somewhat silly example based on the SOE proposal

    module Collections {
      export {
        where: Array.prototype.filter,
        select: Array.prototype.map
      }
    }

    module LolCatzDotCom {
      // imports Array.prototype extensions where and select into this
module
      import {where,select} from Collections;

      var allCatz = someArrayValue;
      // Array extensions are in scope
      var cuteCatNames = allCatz::where(cat => cat.isCute)::select(cat =>
cat.name);
    }

If you still don't get it, give me an example, and I can probably show the
:: equivalent. What is your ideal SOE form? Because my guess is that I can
get it darn close with ::

- Russ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131015/ea394185/attachment.html>


More information about the es-discuss mailing list