Arrow functions as methods

Brendan Eich brendan at mozilla.org
Wed Jul 11 12:28:16 PDT 2012


Irakli Gozalishvili wrote:
> I think more general problem with arrow functions is that they can not 
> capture object on which method call happened:

As I tweeted, this is true of any function that ignores |this| or binds 
it. Nothing new with arrows except the convenience.

We've been around that block and will go again. If we want convenient 
unbound-this function syntax, -> stands ready. I proposed it along with 
=> originally but it was cut by TC39 during our March meeting in order 
to achieve consensus on =>.


> object.foo(bar)
>
> If `object.foo` is an arrow function it has no way of knowing weather 
> it was invoked as a function or a method. Neither it get's reference 
> to an `object`. This makes composition between functional & OO styles 
> components hard.

No, I don't think so. The ability of a function to ignore any parameter, 
including |this|, may frustrate composition, but the contract of an API 
includes mandatory parameterization of any funargs.

People deal with this pretty well in practice, usually by writing 
small-world codebases and wrapping anything unknown that needs 
contract-enforcement along these lines.

> Passing owner `object` of the arrow method as first argument solves 
> this problem, by making
>
> object.foo(bar)
>
> just a sugar for
>
> var foo = object.foo;
> foo(object, bar);

This completely breaks functional abstraction, of course.

f(a)

and

o.m(a)

in JS should never pass more than one actual parameter to the callee, 
however it was declared or expressed.

/be

>
>
> Regards
> --
> Irakli Gozalishvili
> Web: http://www.jeditoolkit.com/
>
> On Wednesday, 2012-07-11 at 10:23 , Irakli Gozalishvili wrote:
>
>> It is true that on could wrap arrow function into vintage one in 
>> order to reuse it as a method, but that is a poor mans workaround. As 
>> of motivation, all I really want is to have a synergy of different 
>> paradigms that are in the language.
>>
>> What I really mean by that is, if one writes in a functional style:
>>
>> var map = (list, f) =>
>>   reduce(list, (result, value) =>
>>      result.concat([ f(value) ]), [])
>>
>> That should be reusable in OO code without any changes or wrappers:
>>
>> List.prototype.map = map
>> List().map((x) => x + 1)
>>
>> Also this complements another idea that I have posted a while ago:
>> https://mail.mozilla.org/pipermail/es-discuss/2012-June/023657.html
>>
>> Where I proposed to make private names functions:
>>
>> var method = Name()
>> method(object, arg) // is just a sugar for
>> object[method](arg)
>>
>> This will enable people to write code in OO or functional style and 
>> make it usable in other style:
>>
>> var map = Name();
>> List.prototype[map] = (list, f) =>
>>   reduce(list, (result, value) => result.concat([ f(value) ]), [])
>>
>> Which one will be able to us in a functional style
>>
>> map(list, f)
>>
>> or in OO
>>
>> list[map](f)
>>
>> Also even if suggested changes to private names won't take of, it's 
>> still can be implemented as a library:
>> https://github.com/Gozala/method
>>
>> There for a way to author functions that also can be used as methods 
>> is relevant. Thin arrows would probably also solve this problem.
>>
>> Regards
>> --
>> Irakli Gozalishvili
>> Web: http://www.jeditoolkit.com/
>>
>> On Wednesday, 2012-07-11 at 09:39 , Claus Reinke wrote:
>>
>>>> function map(f) {
>>>> return this.reduce((result, value) => result.concat([ f(value) ]), [])
>>>> }
>>>> ..
>>>> var map = (list, f) =>
>>>> list.reduce((result, value) => result.concat([ f(value) ]), [])
>>>
>>> Not sure I got your motivation, but would this help?
>>>
>>> function fn(f) { return f(this) } // provide 'this' as explicit arg
>>>
>>> let map = it => f =>
>>> it.list.reduce((result, value) => result.concat([ f(value) ]), [])
>>>
>>> let obj = { map: fn(map), list: [1,2,3] }; // wrap map as method
>>>
>>> obj.map(x => x+1);
>>> map( {list:[1,2,3]} )(x => x+1);
>>>
>>> Claus
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list