Hacker News new | ask | show | jobs
by mbel 3115 days ago
Fat arrow is used to define a lambda function. The left-hand side of the arrow is argument list (_ is used to indicate lack of used arguments) and the right-hand side is the function body. It is equivalent to:

   var currentUrl = function () { return window.location.href; }
2 comments

> _ is used to indicate lack of used arguments

Note that this is a convention, not a requirement for the syntax (like in some other languages).

I've spent at least a few mins troubleshooting why `_.map` (from `lodash` npm module) didn't work inside of a fat function while fixing another developer's defect.

If you use `_` as a valid const/variable identifier anywhere in your codebase, I would recommend you ditch `_ => alert(1)` for something more obscure like `ΓΈ => alert(1)`

It is not the same as that. the fat arrow doesn't change the value of "this" while the function definition does.
Given that "this" isn't referenced in the function, though, it's practically equivalent.
I think one is more efficient memory wise but I'm not sure which one.
The non-fat-arrow one, as it does not need to carry around the context of `this` with it.
Do you have a reference for this? I always thought arrows were lighter weight because their `this` is static rather than dynamic.