Arrow binding

Tab Atkins Jr. jackalmage at gmail.com
Tue Apr 24 10:03:28 PDT 2012


On Tue, Apr 24, 2012 at 9:51 AM, Rick Waldron <waldron.rick at gmail.com> wrote:
> When I read this, I assumed it was a reference to
> this: http://wiki.ecmascript.org/doku.php?id=harmony:object_literals#object_literal_property_shorthands
>
> Which would produce a method named "set" on "box"
>
>> box.set(42)
>> box.get()
>
> 42
>
>
> The example is easily confused.

Ah, that makes sense.  Okay.

In that case, I don't see why one wouldn't just use the same syntax
for both methods:

let box = {
  _value: 0,
  get() { return this._value; },
  set(v) { this._value = v; }
};

There doesn't seem to be a need there for thin-arrow (dynamic this) functions.

(Edit: Oh, I see, leaning on the completion value of thin-arrow
functions here lets you shave a few characters off.  The readability
loss of mixing the two syntaxes doesn't seem worthwhile, though.)

In fact, we can enumerate the different situations functions can
appear, since they're all gaining nice syntax:

1. Method on a class - when we get max/min classes, these'll have a
nice syntax, and will have dynamic-this behavior, just what we want.
2. Method on an object literal - if we have the syntax extension
outlined above, you also get dynamic-this, which we want.
3. Method dynamically attached to an object - here we don't have
special syntax, so we'll still need to use the "function" keyword.
4. Non-method function - you don't want a dynamic this, you want to
lexically close over all your variables.  Fat-arrows do this for you.
(The only reason to want dynamic this on a non-method is to do a
poor-man's currying.  Get a real curry function; this isn't worth the
bugs.)

The only case that isn't receiving special syntax (and thus which
could potentially benefit from a dynamic-this thin-arrow function) is
#3.  However, while #3 is common today, #1 and #2 will eat most of its
share.  I'm not sure that the remaining #3 cases will be worth a
special syntax.

If they are, though, nothing's closing the door on a thin-arrow in the
future.  No reason to block fat-arrow based on this.

~TJ


More information about the es-discuss mailing list