3 ms·
Lambdas make code really easy to read compared to making the reader skip around the code. In addition, it reduces boilerplate code and requires less time to be
by nawitus 11y ago
Lambdas make code really easy to read compared to making the reader skip around the code. In addition, it reduces boilerplate code and requires less time to be spent on naming functions. Of course, it all depends on situation. Simple logic which is not beneficial elsewhere can be implemented as an anonymous function in my opinion.
For example:
if(objects.find(object => object.id === id)) {}
- tragic 11y agoAlso: anonymous function, file x, line y tends to be enough for me.
- coldtea 11y ago>Lambdas make code really easy to read compared to making the reader skip around the code. You can have lambdas that are also named, e.g: if(objects.find(function idCheck(object){ return object.id === id}) Not sure if it's possible with the new ES6 syntax [Edit: apparently it's not, although arrow functions will automatically be assigned a name in some common scenarios].
- Killswitch 11y agoThat defeats the whole purpose of fat arrow functions.
- coldtea 11y agoI'm not sure what purpose you have in mind. The main purposes of fat arrow functions is the auto-binding of this and succinctness. A named fat-arrow function would still retain both, and in fact the feature was considered for ES6 back in 2013, but didn't made it. Syntax would have been like: if(objects.find(functionName(object) => object.id === id)) {}
- vkjv 11y agoIt's also important to note that these are not quite the same thing. If you name a function, it will be hoisted to the top of the scope where a possible naming conflict could occur.