Function declarations with lexical `this`?
David Bruant
bruant.d at gmail.com
Tue Jun 25 00:27:03 PDT 2013
Le 25/06/2013 02:36, Dmitry Soshnikov a écrit :
> On Mon, Jun 24, 2013 at 5:00 PM, Mark S. Miller <erights at google.com
> <mailto:erights at google.com>> wrote:
>
> On Mon, Jun 24, 2013 at 4:46 PM, Axel Rauschmayer
> <axel at rauschma.de <mailto: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 = () => {}
You'd be surprised. I've read lots of files from different people where
I was left wondering "why did he/she choose 'var x = function(){...}'
over 'function x(){}'". In the end, there was no good reason.
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130625/d14eb21c/attachment.html>
More information about the es-discuss
mailing list