Arrow Functions and this
Concise function syntax, implicit returns, and how arrow functions change what
this means.
An arrow function is a shorter way to write a function expression, which
Functions and Parameters covered. It also treats this
differently, and that is the real difference.
The shorthands
An arrow function drops the function keyword. The parameters come first,
then =>, then the body.
The first three lines all print Andor. The last prints undefined, because
braces after => start a body, and that body never returns.
The short form is called an implicit return. You will see it everywhere in React, because so much of React is short functions passed to other code.
Two parameters always need parentheses: (show, kind) => show.title. Only a
single parameter may drop them.
Returning an object literal
There is one trap. Braces after => are read as a body, not as an object. To
return an object, wrap it in parentheses.
makeShowBroken prints undefined. JavaScript read title: title as a
label, not as a property. makeShow prints the object.
With two properties the broken version does not even run: a body cannot hold
title: title, year: 2022. One property hides the mistake, which is what
makes it worth knowing.
this is inherited
A plain function gets its own this, decided by how it is called. An arrow
function has no this of its own. It uses the this of the place where it
was written.
Both inner functions sit in the same place, inside a method of show. The
plain one still loses the show, so it prints not the show. The arrow keeps
it and prints Severance.
The rule of thumb: arrows for callbacks, function for methods. In React you
hand arrows to other code all the time, and an arrow does not care who calls
it.
Common mistakes
- Braces when you meant an object.
(title) => { title: title }givesundefined; write(title) => ({ title: title }). - A body with no
return. Braces after=>make a body, so the value has to be returned. - An arrow as a method. An arrow has no
thisof its own, sothis.titlewill not find the object; write methods withfunction.
Exercise
makeBooster should return a function that adds amount to a rating. Both
lines print undefined instead.
Both calls should print:
Severance: 9.7
Andor: 8.5
Hint: look at the braces after the arrow.