Fat Arrow Functions

In ES6 it is common to see fat arrow functions. This way of writing functions has a syntax like this:
const MyFunction = () => { return 1;}
That is equivalent to:
Function MyFunction() { return 1; }
You’d call such a function like this:
MyFunction();

Functions created with the arrow syntax have the feature that the lexical “this” is remembered between invocations. You do not have to declare some extra variable to hold a consistent “this”.

Also, “yield” does not work in fat arrow functions so you cannot use them as generators.

Most likely you’ll want to use fat arrow functions in lambdas (functions that return immediately) and for functions that need to work with parent scoped “this.”

This entry was posted in Articles, DDJ, Lrw updates, Technology and tagged , . Bookmark the permalink.