liveBook · Manning

Arrow function

As explained in Secrets of the JavaScript Ninja, Third Edition

The => token used to define an arrow function in JavaScript.

Callbacks are common in JavaScript, but a traditional function expression can be verbose—and its `this` can change when another function invokes it. An arrow function uses `=>` between its parameters and body. It offers shorter syntax and takes `this` from its surrounding context rather than from the way it is called. section 5.1.2 section 6.3.2

A shorter function

`const greet = name => "Greetings " + name;` returns the expression without braces or a `return` keyword. A single parameter can omit parentheses; zero or multiple parameters require them. With a braced body, use an explicit `return` when you want to return a value: `const greet = name => { const prefix = "Greetings "; return prefix + name; };` section 5.1.2

The difference from an ordinary function matters most when functions are passed around. Passing an object's method directly as an event callback separates the function from its object, so expecting `this` to remain that object can produce a broken click count. An arrow function instead keeps the `this` of the context in which it was defined. Calling it with `call` or `apply` cannot change that value; `bind` cannot give it a different `this` either. section 6.3 section 6.3.2

Keep the method call intact

For an ordinary object with `clickHandler`, register `() => { buttonObject.clickHandler(); }` as the event callback. The arrow is a wrapper: it calls the method through `buttonObject`, preserving that method call's context. Do not replace the object's `clickHandler` itself with an arrow expecting `this` to mean `buttonObject`; an ordinary object literal does not create a lexical context. section 6.3.2

Use an arrow when its compact expression form or inherited `this` is what you need. When a function must receive `this` from the object used to invoke it, keep the distinction between an arrow and an ordinary method in mind. section 5.1.2 section 6.4

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains parameter and body forms, then works through lexical `this` and its behavior with `call`, `apply`, and `bind`.
Examples
Uses greeting and hypotenuse functions alongside an event handler whose click count breaks when its method is passed unbound.
What it assumes
Assumes familiarity with functions and callbacks while defining arrow syntax and explaining why callback invocation affects `this`.
What you can do after
Practice/perform writing concise callbacks and choosing a wrapper when an ordinary object's method needs its invocation context.
How it connects
Connects arrow functions to function expressions, callbacks, method invocation, lexical `this`, binding, and class fields.

Also explained in

Compare how 9 books explain arrow function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.