This is the second in a series of posts where I explore all the new stuff es6 has to offer, come along and learn vicariously!
If you’ve used any recent js library or framework you might have stumbled upon fat arrows ( or arrow functions) that use the following syntax =>
See the Pen ES6 Sampler - Fat Arrow pt1 by Eugenio - Keno - Leon (@k3no) on CodePen.
Well ok then arrows seem to replace the function syntax , but how is that an improvement ?
Compact Syntax in anonymous functions
See the Pen ES6 Sampler - Fat Arrows pt2 by Eugenio - Keno - Leon (@k3no) on CodePen.
Anonymous functions (nameless functions for instance) are everywhere in javascript, with the arrow syntax you can ommit the word function.
Another very common instance, is the use of functions as arguments for other functions, with arrow functions the syntax can become somehow clearer.
See the Pen ES6 Sampler - Fat Arrows pt3 by Eugenio - Keno - Leon (@k3no) on CodePen.
.this and scope with arrow functions
Have you ever lost track of your global variables or wanted to use a local variable but couldn’t figure out which this to use ? , arrow functions help by making things a little more concise, consider the following cases:
See the Pen ES6 Sampler - Fat Arrows pt4 by Eugenio - Keno - Leon (@k3no) on CodePen.
I personally believe in a verbose syntax when writing javascript to make things easy to a wider audience,but as more and more developers and libraries use newer features, it is important to at least know how to recognize what’s being written, so I would like to finish this very small introduction to arrow functions with other cases one might encounter:
Javascript methods with callbacks :
See the Pen ES6 Sampler - Fat Arrows pt5 by Eugenio - Keno - Leon (@k3no) on CodePen.
JQuery:
See the Pen ES6 Sampler - Fat Arrows pt6 by Eugenio - Keno - Leon (@k3no) on CodePen.
The new es6 arrow syntax brings a more compact/clear way of writting functions, It also helps with scope in some cases, while using them is up to you, recognizing them is no longer optional, so it pays to learn them.
() => You will learn to love me.
Best,
Keno