# Named Arrow Declaration

**URL:** <https://es.discourse.group/t/named-arrow-declaration/903>\
**Category:** 💡 Ideas\
**Created:** [August 6, 2021, 1:19pm UTC](https://es.discourse.group/t/named-arrow-declaration/903 "2021-08-06T13:19:41Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [August 6, 2021, 1:19pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/1 "2021-08-06T13:19:42Z")

</div>

I've found myself using this invalid syntax in my notes:

```javascript
const mix( a, b, t ) => ( t-a.t )*b.x + ( b.t-t )*a.x;

```

It abbreviates the assignment.

```javascript
const mix = ( a, b, t ) => ...

```

This abbreviation does not suffer from the same ambiguity as omiting the `function` keyword. Is there a chance it could someday be valid?

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [August 6, 2021, 1:30pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/2 "2021-08-06T13:30:53Z")

</div>

It's an interesting idea. But proposals like this often have a hard time getting through. They add a little more complexity to the language, and another thing to learn, and the only benefit is removing an "=" from your source code.

Though, every once in a while a shorthand syntax goes through (we use `{ x, y }` instead of `{ x: x, y: y }`, and arrow functions themselves are mostly meant to be a shorthand for `function() { return ... }`, etc)

---

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [August 6, 2021, 1:48pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/3 "2021-08-06T13:48:21Z")

</div>

So, "every once in a while" might mean maybe?

I can't imagine a proposal devoted to this one tiny change.  
I wonder if there exists a proposal that could include this.

Could also add shorthand property declaration for arrow functions.  
When learning the language, I would naturally expect this to work:

```javascript
const a = { m(x) => x+1 }

```

Given that this works:

```javascript
const a = { m(x) { return x+1 } }

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [August 6, 2021, 1:59pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/4 "2021-08-06T13:59:10Z")

</div>

New syntax is an incredibly massive change that would absolutely require a proposal.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [August 6, 2021, 2:14pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/5 "2021-08-06T14:14:22Z")

</div>

Some proposals only contain minor changes (e.g. [this proposal](https://github.com/tc39/proposal-accessible-object-hasownproperty) defines a single function that anyone could define with one line of code) - there's nothing wrong with having things like this be in their own proposal.

> [@0X-JonMichaelGalindo](#):
>
> So, "every once in a while" might mean maybe?

Maybe ... I personally would be against it. I don't think the slight added language complexity is worth the slight abbreviation. I guess I don't find that syntax intuitive enough. But others would probably disagree with me.

---

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [August 6, 2021, 2:18pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/6 "2021-08-06T14:18:11Z")

</div>

In your opinion, is this worth pursuing?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [August 6, 2021, 3:11pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/7 "2021-08-06T15:11:35Z")

</div>

I'd use it - rather than relying on function name inference, and in places like `export default` or function arguments where an arrow function can't otherwise get a name - but I don't think it's worth the syntax churn just to avoid using a normal named function.

---

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [August 6, 2021, 3:24pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/8 "2021-08-06T15:24:42Z")

</div>

Hmmm.

Well, I'll leave it at: I hope some proposal eventually includes this for a more justifiable reason.

---

<div class="post-metadata">

**Author:** ![dario-piotrowicz](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/dario-piotrowicz/32/1363_2.png) [@dario-piotrowicz](https://es.discourse.group/u/dario-piotrowicz)\
**Post date:** [April 30, 2022, 4:54pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/9 "2022-04-30T16:54:32Z")

</div>

Hi there, sorry for intruding in this discussion which hasn't been active for a while.

I just stumbled across this thread as I was actually thinking of opening one like this myself

I just wanted to mention another couple of reasons why this could in my opinion be quite a nice addition

Firstly, I would just like to say that for via `function` we can actually define both named and anonymous functions, so it would actually stand to reason to me that the same should be possible with arrow functions as well (without the need to rely on name inference as @ljharb mentioned).

Second (which sort of adds to what @ljharb mentioned), I think that it would be very good for callbacks, those are currently always anonymous (unless you go out of your was and use `function` instead) and they usually create various anonymous calls in the call stack which aren't as easily debuggable.

As a very simple example, let's say I have

```javascript
myArray.forEach(el => { return el+1; });

```

inside the callback function the call stack would just say that the function is anonymous, and to actually have that function named we'd have to do:

```javascript
myArray.forEach(function addOne(el){ return el+1; })

```

(or have define the `addOne` arrow function in a previous statement)  
it would be quite neat if we could instead do:

```javascript
myArray.forEach(addOne(el) => { return el+1; });

```

Of course this is an extremely simple example but with frameworks/libraries which highly depend on callbacks (I am for example thinking of rxjs) it could actually quite improve the developer experience (and defining the arrow functions prior to the callback just so that they can name-inferred could just create clutter)

Anyways I just wanted to mention that 🙂

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [May 8, 2022, 4:40am UTC](https://es.discourse.group/t/named-arrow-declaration/903/10 "2022-05-08T04:40:40Z")

</div>

Nit: `async` arrow functions conflict with that.

---

<div class="post-metadata">

**Author:** ![dario-piotrowicz](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/dario-piotrowicz/32/1363_2.png) [@dario-piotrowicz](https://es.discourse.group/u/dario-piotrowicz)\
**Post date:** [May 8, 2022, 8:24am UTC](https://es.discourse.group/t/named-arrow-declaration/903/11 "2022-05-08T08:24:19Z")

</div>

good point! 😓

---

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [May 8, 2022, 6:35pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/12 "2022-05-08T18:35:14Z")

</div>

It would be so nice!

For debugging purposes this is the best workaround I've got:

```nohighlight
const DEBUG = { ... }

myArray.forEach( DEBUG.addOne = el => { return el+1; } );

```

If that function throws (e.g. if the array contains a BigInt), Firefox will now trace that function as `addOne`, and Chrome will trace it as `myArray.forEach.DEBUG.addOne`.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 8, 2022, 8:23pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/13 "2022-05-08T20:23:26Z")

</div>

That seems like it's a lot more difficult than `myArray.forEach(function (el) { return el + 1; })`.

---

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [May 8, 2022, 9:23pm UTC](https://es.discourse.group/t/named-arrow-declaration/903/14 "2022-05-08T21:23:26Z")

</div>

In Firefox, the error origin for that code was simply `<anonymous>`, same non-traceable problem as the anonymous arrow function. But if I add a name, it is hoisted / pollutes the scope.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [May 9, 2022, 1:23am UTC](https://es.discourse.group/t/named-arrow-declaration/903/15 "2022-05-09T01:23:51Z")

</div>

> [@0X-JonMichaelGalindo](#):
>
> But if I add a name, it is hoisted / pollutes the scope.

`function` expression names aren't added to the containing scope. That only happens for declarations (when the function is not being used as an expression).

```javascript
myArray.forEach(function myFunc(el) { return el + 1; })
console.log(myFunc) // Error: not defined

```

---

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [May 9, 2022, 1:54am UTC](https://es.discourse.group/t/named-arrow-declaration/903/16 "2022-05-09T01:54:02Z")

</div>

Indeed they are not. 😑

I really did test it in the console before I posted that (essentially the same code you posted), and the function was defined. My console scope must have been dirty from some earlier line. (This thread has also wandered off-topic at this point.)

---

<div class="post-metadata">

**Author:** ![graphemecluster](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/graphemecluster/32/957_2.png) [@graphemecluster](https://es.discourse.group/u/graphemecluster)\
**Post date:** [May 25, 2022, 9:19am UTC](https://es.discourse.group/t/named-arrow-declaration/903/17 "2022-05-25T09:19:10Z")

</div>

> [@0X-JonMichaelGalindo](#):
>
> ```javascript
> const a = { m(x) => x+1 }
> 
> ```
> 
> Given that this works:
> 
> ```javascript
> const a = { m(x) { return x+1 } }
> 
> ```

I personally think that it is worth allowing this syntax in a class because `class { m = x => x + 1 }` does behave differently from `class { m(x) { return x + 1 } }`: only the latter puts the method into the prototype. Sometimes I find myself really wanting to inline a class method.
