Scoped binding of a method to an object

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Oct 14 18:09:20 PDT 2013


On Oct 14, 2013, at 3:58 PM, Brendan Eich wrote:

> Russell Leggett wrote:
>> It doesn't use dots, so it won't mask the difference between the normal prototype chain with some additional scoped binding (for good or ill), but along with it comes the clarity and comfort of lexical binding and also the potential use of the module system.
>> 
>>    import {shuffle,each,filter} from "underscore2";
>>    myArray::shuffle();
> 
> This is really winning, thanks again.
> 
> We already have good motivation for :: anyway, as sugar for bind. This gives relief to the OO side of the expression problem trade-off by allowing lexical bindings to be composed with method calls -- beautiful. No third scope axis / lookup parameter!

Speaking from the perspective of someone whose probably has permanent OO brain damage, it doesn't do a lot for me.

The reason I "invoke a method" on an object is because I want to do polymorphic dispatch on the method name.
 myArray::shuffle() doesn't do that for me.  No polymorphic dispatch.  If myArray actually does have a shuffle method it isn't called. If that sort of direct function invocation is what I want,  I'll just code a function call. No method  invocation syntax and no |this| value is need. 

I still don't get why so many JS programmer with a FP orientation want  to do things with the |this| binding.   |this| is for us OO geeks, if you are doing FP you don't need it.  If you want to write shuffle, each, and filter functions just code them as normal functions passing the collection as the first argument.  If I want to use you functions as a methods on one of my objects I'll just code something like:
    class {
       shuffle() {return shuffle(this)}
    }

Now, what might be useful would be :: that has approximately this semantics

import {shuffle,each,filter} from "underscore2";
myArray::shuffle();

desugars as

 do {let _method = myArray[shuffle.name];
   _method ? _method : shuffle}.call(myArray);

in other words, if myArray has a 'shuffle' method, call it; otherwise call the default shuffle method that I'm providing. 

Allen


More information about the es-discuss mailing list