On Jun 20, 2011, at 4:56 PM, Axel Rauschmayer wrote:

>>> I meant: One can already write methods (functions with dynamic |this|) in a 
>>> very concise manner, thanks for Allen’s object literal extensions. Then you 
>>> have to ask: Do we really need the dynamic this arrow ->, or can we make do 
>>> with just the lexical this arrow =>.
>> 
>> We do not propose to cripple the shorter syntax. The dynamic-this use-cases 
>> for functions are at least as common as "var self=this;... function(){... 
>> self...}" use-cases for lexical this, or roughly about the same (in my 
>> experience -- anyone have data?).
> 
> 
> Are functions that depend on dynamic |this| ever *not* methods?

Sure. The main use-case is constructors (and you can make a function do 
double-duty, as a constructor or a function called without 'new').

Also, some times, people write functions not used as methods but called via 
.call or .apply. It happens.

On the other side of the fence, it's not uncommon to have "methods" that want 
lexical or no |this|. In the closure pattern, you often see code like so:

  function Car(make, model, key) {
    function validate(key) {...}
    function cancelAlarm() {...}
    return {
      start: function (key) {
        if (validate(key)) {
          cancelAlarm();
          ...
        }
      },
      lock: function ...,
      unlock: function ...,
      // etc.
    };
  }

The methods of the object literal do not use |this|, but if they did, they 
might prefer to use => and avoid the hazard of an accidental |this| reference 
within one of their bodies being re-bindable.


> Wouldn’t you always want to use an object literal for methods, especially if 
> some features (such as |super|) depend on it?

Probably. But so what?

We're not making mandatory syntax either way with function, so why should we 
with arrow?


> Isn’t it then a case of
>        { foo: (x) -> { ... } }

You mean => here, I think.


> versus
>        { foo(x) { ... } }

Arrows are not in yet. The method syntax looks like it is the most agreed-to 
among the 
http://wiki.ecmascript.org/doku.php?id=harmony:concise_object_literal_extensions
 at this point.

BTW, if Arrows make it then, as with binding forms such as const and let, the 
property assignment shorthand will be

    { foo(x) => ... } // or =>, and with ... an expression or block


> Maybe I just like the distinction introduced by block lambdas too much:
> - dynamic this --> existing functions and methods
> - lexical this --> new, more compact construct, mainly used as the argument 
> of functions and methods.

Block-lambdas are not arrows. Arrows are just syntax and try to shorten 
function usage in general (including the .bind or var self=this; idiom). 
Block-lambdas must preserve Tennent's Correspondence Principle, so they have no 
choice but to treat |this| lexically.


> This distinction works well as a rule of thumb and keeps things easy to 
> explain.

It's not that simple: existing functions can and do use various binding hacks 
to make |this| or a substitute "lexical". Anyway, functions have |this| 
parameters, whether created by old function or new arrow syntax.

/be
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to