A few arrow function specification issues

Axel Rauschmayer axel at rauschma.de
Sat Apr 21 13:28:11 PDT 2012


> I am in agreement with Angus on this one.
> 
> If there is no skinny arrow, hard |this| binding should not be part of the spec. Fat arrow functions should default to lexical |this|, but if there is no skinny arrow, it should not be a hard binding and call/apply should be allowed, as well as bind,

IMHO, the spec got it exactly right. We have a chance to really simplify things and introduce a clear distinction:

- Methods – dynamic `this`: Use a concise method definition inside an object literal (or, hopefully, inside a class declaration).
- Non-method functions – lexical `this`: Use an arrow function.

Anything else is rare (see Kevin Smith’s JS code survey) and non-methods with dynamic this are usually an anti-pattern. If you really need it, there are always old-school functions.


As for switching between dynamic `this` and lexical `this` depending on whether a function is called as a method or as a function. At first glance, it sounds like a good idea, like the best of both worlds. However, I’m with Domenic Denicola: it’s too brittle – a function shouldn’t work radically (and silently!) different depending on how it is invoked. Switching to this = undefined is bearable for old-school non-method functions, but switching to lexical this will lead to many hard-to-debug errors.

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

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


More information about the es-discuss mailing list