Arrow functions as methods
brendan at mozilla.org
Wed Jul 11 17:49:04 PDT 2012
There's still some awkwardness: obj::obj.foo requires repeating obj.
Irakli's mail suggests obj::foo somehow scoping foo to obj if possible,
but this smells of 'with'.
Gordon Brander wrote:
> On Jul 11, 2012, at 2:58 PM, Brendan Eich wrote:
>> Irakli Gozalishvili wrote:
>>>> 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 =>.
>>> Yeah I'm aware of that, but problem as I see is that new fat arrow functions can not fully replace existing functions.
>> That is not a problem, or rather: not a problem with fat arrows.
> I would like to loop this conversation away from fat arrow and back to discussing a convenient way to specify flexible `this`.
> Irakli suggested:
> o.foo(a, b)
> as sugar for
> foo(o, a, b)
> ...which is how Python and Lua handle `this`. Treating method invocation as sugar this way allows you to think of `this` as being a category of data on which you operate, rather than a class instance you have access to. It's kind of a functional twist on OOP that makes it easy to write functions that can be used in *both* OO and functional style.
> But back to ES... I think the motivation here may be to a convenient syntax for treating `this` as "soft" so you can assign methods to other objects, or easily use methods in a functional style. This is sort of how functions work today with call and apply (only you can't pass the call function object around without losing its function context).
> I noticed an alternative solution to this problem in the ES Wiki: http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax#deferred (see "optional leading `this` initializer"). As I understand it, that would give us some sort of special delimiter for specifying a soft-bound `this` context on a thin-arrow, e.g. in pseudo-code:
> foo(o | a, b)
> ...or some-such. Irakli, would such a feature resolve your use-case?
More information about the es-discuss