liveBook · Manning

First-class function

As explained in Secrets of the JavaScript Ninja, Third Edition

A function that can be treated as a value, including being assigned, passed as an argument, or returned.

Need to choose what happens when an event fires or how an array is sorted? In JavaScript, a function can be handled as a value: stored, passed to other code, or returned. It is an object with one extra capability—it can be invoked. This differs from the assumption that functions are only named pieces of code called directly. section 5.2.1 section 5.2.2 section 1.1

A function value can be:

  • Assigned to a variable, an array entry, or an object property. section 5.2.1
  • Passed as an argument or returned as a result. section 5.2.1
  • Given properties of its own, created and assigned dynamically. section 5.2.1

Pass a callback, then call it

`function call(callback) { return callback(); }` `call(function () { return "ready"; });` The anonymous function is created where an expression can appear and passed to `call`. Inside `call`, the parameter refers to that function; `callback()` invokes it. Passing `callback` and writing `callback()` are different actions. Defining a one-use function at the call site also avoids giving it an unnecessary name elsewhere. section 5.2.1 section 5.2.2

const values = [0, 3, 2, 5, 7, 4, 8, 1];
values.sort((value1, value2) => value1 – value2);

An array’s `sort` call receives an arrow function as its comparator, supplying the comparison behavior without requiring you to implement the sorting algorithm. section 5.2.2

The same pattern supports event handlers: code registers a function, and the browser calls it when the event occurs. First-class functions also provide a starting point for composing functions, though that broader functional-programming style is a separate topic. The practical rule is simple: keep the function as a value when other code needs to store or call it; add `()` when you mean to invoke it now. section 5.2.2 section 5.2.1 section 5.6

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Defines object-like function capabilities and traces a callback invocation, but defers a fuller treatment of functional programming.
Examples
Uses a passed text-returning callback, an inline callback, a mousemove handler, and an array-sort comparator.
What it assumes
Assumes basic familiarity with variables, objects, and function calls while defining first-class status before exploring callbacks.
What you can do after
Practice/perform storing functions and passing callbacks to JavaScript code that invokes them later.
How it connects
Connects first-class functions to callbacks, function expressions, arrow functions, event handlers, comparators, and functional programming.

Also explained in

Compare how 2 books explain first-class 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.