Live data from Hacker News

Fat arrow functions in Javascript

robcee.net

1–10 of 48 posts

Re: Fat arrow functions in Javascript

#5
First and probably most useful is that they gain the lexical scope of the environment they’re defined in.

Nit: technically, all Javascript functions are lexically scoped. The fat arrow permanently binds the value of "this", which is sort of like modifying the scope, but again not technically because "this" is a special keyword and not a variable.

Re: Fat arrow functions in Javascript

#6
post #5

First and probably most useful is that they gain the lexical scope of the environment they’re defined in. Nit: technically, all Javascript functions are lexically scoped. The fat arrow permanently binds the value of "this", which is sort of like modifying the scope, but again not technically because "this" is a special keyword and not a variable.

that's a good nit. I'll drop it. Thanks!

Re: Fat arrow functions in Javascript

#8
While this is pretty cool, I'm not really a fan of this. There is less to type, but I end up spending more time reading what the code is doing since there's more than one way to do it. (With optional parens, optional braces, and special syntax for returning objects in a single line).

I would have preferred a different function keyword that had lexical this binding.

Re: Fat arrow functions in Javascript

#9
post #8

While this is pretty cool, I'm not really a fan of this. There is less to type, but I end up spending more time reading what the code is doing since there's more than one way to do it. (With optional parens, optional braces, and special syntax for returning objects in a single line). I would have preferred a different function keyword that had lexical this binding.

Not sure I agree with you. After using them for a few months, they feel pretty natural, light-weight and used close to where you define them. There's actually less thinking required when using a fat-arrow function because you don't really have to worry about the scope.

Re: Fat arrow functions in Javascript

#10
Frankly the fat arrow syntax adds so little value that it's not worth adding new syntax to JavaScript, just my humble opinion.

The fat arrow seems to be primarily about easier scoping when using this - so it's trying to patch the single most broken feature a language ever had.

The existing function syntax is fine, rather just avoid using "this" at all in your code.

Post reply on HN