arrow syntax unnecessary and the idea that "function" is too long

Brendan Eich brendan at mozilla.com
Sun May 15 14:41:37 PDT 2011


On May 15, 2011, at 1:49 PM, Faisal Vali wrote:

> Could I trouble you to confirm or clarify the semantics of the
> following constructs*:
> 
> let f = -> -> 3; // well-formed?
> let x = f()();  // x is equal to 3 right?

Sure, unambiguous. Same as in CoffeeScript:

coffee> f = -> -> 3;
[Function]
coffee> f()()
3


> let global = this;
> 
> let o = {};
> o.fat  = => this;
> o.thin = -> this;
> 
> assert( o.fat() === global);  // true?

Sure, or (no need for global) assert(o.fat() === this).


> assert( o.thin() === o); // true?

Yes.


> o.thin_then_fat = -> => this;

This example is given in http://wiki.ecmascript.org/doku.php?id=strawman:arrow_function_syntax -- look for "method:".


> o.fat_then_thin = => -> this;
> 
> 
> assert(o.thin_then_fat()() === o); // true?

Right, as in the strawman.


> assert(o.fat_then_thin()() === global); // true?

No. Since the proposed syntax is just syntax, you can expand it and evaluate:

o.fat_then_thin = (function () { return function () { return this; }; }).bind(this);

So the first call,

o.fat_then_thin() indeed returns function () { return this; } from a function in which |this| was bound to the outermost |this|, aliased via |global|.

But that (function () { return this; }) result is then immediately invoked and the |this| it receives, per ES5 strict and Harmony, is undefined.

That's why I focused on the thin-then-fat example in the strawman. To show fat-then-thin with |this| dynamically bound on the second call (to the "inner" thin arrow function), you might need to store the return value of the first (outer, o.fat_then_thin()) call as a property value, and call it that way. Or via .call/.apply. Or as you did here, but it is less clear to have an undefined |this| pop out at the end.

Again, http://wiki.ecmascript.org/doku.php?id=strawman:arrow_function_syntax is "just syntax". Therefore since -> does not bind |this|, that parameter is bound dynamically depending on the callee sub-expression of the call expression. Same as for long function syntax with ES5 strict semantics.

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


More information about the es-discuss mailing list