Shaping functions! — A JavaScript journey #3

Yann

Yann


Nov 28th 2018 in code

We live in the era of JavaScript Functional Programming, but I still asked myself sometimes if my way of declaring function is the “good” one. It’s one of those early basic things that we learn first and that we are using all the time, but without really knowing why and how it is so.

Since ES2015, I’m a big fan of minimal arrow functions like :

Still, it’s time for checking the basics and current status of functions creation ! (And also if I’m not totally wrong about the default practice above)


Your options

I found a very interesting and only 10-year-old Stack Overflow question about this subject and the following explanations are inspired by it. So in JavaScript, there is more than 7 ways of creating functions and I will present (almost) all of them.


Brief

For each of the following examples, we are going to create a pure function which will return the sum of all the arguments of type number that we pass in.


Function Declaration

Maybe the one that everybody learns first and very close to a lot of other programming languages :

“A function declaration is processed when execution enters the context in which it appears, before any step-by-step code is executed.”

This is a very important point, because unless almost all of the following variants, here you can basically call a function above it’s declaration. Not the best practice from my point of view, but still.

Like almost anything in ES2015+, it’s block scoped. So it’s only available on its context (another function, condition statement, etc).


Anonymous Function Expression

Very close to the “new” arrow function (see below), it consists of assigning an anonymous method to a variable. It’s evaluated when it’s reached in the step-by-step execution of the code, so don’t try to call it before as the previous example; it will return an undefined function error.

— OK, but it will be hard to track errors because of the anonymous nature of your function, bla bla bla

— Wrong !

It has been the case in the old times, but since ES2015+ sum.name will return you "sum" and not an empty string like it was before. So there is less downsides of using classy Anonymous Function Expression.


Named Function Expression

It’s a combination the Function Declaration and Anonymous Function Expression. Named Function Expression or NFEs solve the anonymous “issues” back in the days, but has also the benefits of providing a shorter method name in it context, very useful for recursions.


Arrow Function (ES2015+)

[SPOILER ALERT] My favorite :yum:

Simple, sober, clean, beautiful and so much more, this form of function creation is almost like the Anonymous Function Expression, but without its own context. It means that this will refer to it parent context.

Basic rule :

  • Want a context (jQuery stuff) -> Anonymous Function Expression
  • Don’t care -> Arrow Function


Accessor Function Initializer (getter, setter)

Not frequently used, it is still very useful when you want to easily access an object’s attribute and do something with it.


Method Declaration in Object Initializer (ES2015+)

A basic object’s function attribute with its shorter ES2015+ form.


Constructor and Method Declarations in class (ES2015+)

With the popularity of React and the usage of Class, this form of function creation had become really popular.


Conclusion

We saw almost all the forms of function creation (sure it exists others) and we can all agreed that there is not one better than another, because it depends on the context and the purpose.

The way of using by default the Anonymous Arrow Function for standard creation is also not that bad. The downside of anonymity of having difficulties to trace back an error is not more an issue from ES2015+. Personally, I found this “new” form of function creation very elegant, minimalist and self-rewarding to use.

In a Functional world without any real downsides of preferring a form more than another, pick the nice one :laughing: