Shorthand for "function" keyword

Laurentiu Taschina source.spider at gmail.com
Fri Nov 10 14:58:05 UTC 2017


Doesn't work if it's not pre-declared. [1] (see jsfiddle, a bit too long to
post here)

So useless for callback usage, which a very big chunk of the use cases.

While you can argue to kind-of get enough information (it says it's "each"
in chrome) with the other way, and having a name there is just preference,
the function version does produce better results by providing more
information by letting you pick the name yourself. Such as to say "it's an
each that does thing X", not just "it's an each." It doesn't make sense to
penalize people with a harder to work syntax when trying to do it like
that, since it makes the code intent easier to read by both humans and
browsers.

As a general nicety it's also a bit easier to work with when refactoring.

Consider converting this...

each(item, () => {
// ...
});

each(items, function createSum() {
// ...
});



...into this...

function createSum() {
// ...
}


each(items, createSum);


Everyone can refactor it easily when the name is already there. You just
have to move it up, or outside, or wherever.

--

[1] https://jsfiddle.net/89t198uu/1/

On Fri, Nov 10, 2017 at 3:38 PM, T.J. Crowder <
tj.crowder at farsightsoftware.com> wrote:

> On Fri, Nov 10, 2017 at 1:11 PM, Laurentiu Taschina
> <source.spider at gmail.com> wrote:
> >
> > I would like to propose the standard allow for shorthand form for
> > the normal "function" keyword.
> >
> > ...
> >
> > Advantages & Motivation
> >
> > only regular functions can have names, but being so clunky people
> > are discouraged from using the syntax in favor of short but
> > anonymous arrow functions; this is detrimental for stack traces
> > and debugging
>
> That's a myth. The following function has the name `foo`, every bit as
> much as `function foo() { ... }` would:
>
> ```js
> const foo = () => {
>     throw new Error();
> };
> console.log(foo.name); // "foo"
> foo();                 // Stack trace will show the name "foo"
> ```
>
> [Try it here][1] on any vaguely-recent version of Chrome or Firefox.
>
> This was first defined in ES2015, the same spec that defined arrow
> functions; search [the spec][2] for SetFunctionName to see all the various
> places it applies. The short version is: If it's assigned to just about
> anything but a property on a preexisting object and the name of the thing
> it's being assigned to can reasonably be used as a name, that becomes the
> name of the function. So `obj.foo = () => { }` doesn't assign a name (due
> to [information leak concerns][3]), but assigning to a constant, variable,
> property within an object initializer, parameter as default value, etc.,
> all do on compliant implementations.
>
> I think the only thing missing is hoisted declarations for non-`function`
> functions (arrow functions have no declaration form), which doesn't
> immediately seem like sufficient cause for a new keyword (but that's not my
> call).
>
> -- T.J. Crowder
>
> [1]: https://jsfiddle.net/dfpdn0cj/
> [2]: https://tc39.github.io/ecma262/
> [3]: https://esdiscuss.org/topic/name-anonymous-functions-on-property-
> assignments#content-20
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171110/9544fe78/attachment.html>


More information about the es-discuss mailing list