Function declarations with lexical `this`?
Dmitry Soshnikov
dmitry.soshnikov at gmail.com
Mon Jun 24 17:36:16 PDT 2013
On Mon, Jun 24, 2013 at 5:00 PM, Mark S. Miller <erights at google.com> wrote:
> On Mon, Jun 24, 2013 at 4:46 PM, Axel Rauschmayer <axel at rauschma.de>wrote:
>
>> Sorry for bringing this point up, again. It is a minor point, but details
>> matter if ECMAScript 6 is supposed to feel consistent.
>>
>> In general, I like how ECMAScript 6 has evolved functions. Before,
>> functions played three roles:
>>
>> 1. Constructor
>> 2. Method
>> 3. Non-method function (where you want lexical `this`)
>>
>> In ES6, we have classes for #1 and method definitions for #2 (in both
>> object literals and class literals). Furthermore, arrow functions replace
>> function expressions and have lexical `this`. What is missing is a function
>> declaration with lexical `this`.
>>
>> What’s the best way to solve this and to eliminate the pitfall of dynamic
>> `this` (at least for beginners)? Tell people to const-bind arrow functions?
>>
>
> IMO, yes.
>
>
>
>> We’d lose hoisting, though.
>>
>
> yes. I don't think this will actually be a problem in practice.
>
>
>
>> I also wouldn’t want to lose the symmetry function declaration/generator
>> declaration and method definition/generator method definition.
>>
>
> I do sometimes wish there was a natural place to put a "*" on an arrow
> function, and sometimes I don't wish it. In any case, it's not gonna happen
> in ES6.
>
>
>>
>> IMO, we need a consistent story for ES6 in this area.
>>
>
> const foo = (a,b) => a+b;
>
For simple procedures-like function still
function doAction() {
...
}
looks more familiar for programmers than
const doAction = () => {}
(notice also this ugly empty parameters thing = () =).
So I wouldn't kill function declarations and left the dynamic this there
(which still can be used).
=> functions are mostly for one-time use, and short functions (even if the
have blocks). To avoid spaghetti code, usually if the function is big it's
moved outside instead of using in-place.
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130624/a64dae80/attachment.html>
More information about the es-discuss
mailing list