liveBook · Manning

First-class object

As explained in Functional Programming in JavaScript

A function value that can be assigned, passed as an argument, and returned like other values in a language.

A first-class function is a function value that JavaScript treats like other objects. You can assign it to a variable, store it as an object property, pass it as an argument, or return it from another function. section 2.3

This makes behavior reusable and replaceable. Instead of hard-coding what a loop does, pass an action, selector, or operation into a general-purpose function. section 2.3 section 2.5

Pass an operation as a value

function applyOperation(a, b, opt) { return opt(a, b); } const multiplier = (a, b) => a * b; applyOperation(2, 3, multiplier); // 6 Here, multiplier is not called when it is passed. The function itself becomes data that applyOperation can invoke later. section 2.3

var square = function (x) {           #1
   return x * x;                      #1
}                                     #1
var square = x => x * x;              #2

Compare a traditional anonymous function expression with its arrow-function form. section 2.3

How Functional Programming in JavaScript covers it

How deep it goes
Defines function objects, variables, properties, constructors, and arguments, then stops short of detailed execution internals.
Examples
Demonstrates variable assignment, object methods, Function constructors, sorting callbacks, operation arguments, and returned functions.
What you can do after
Apply it by passing operations, selectors, printers, and callbacks into reusable JavaScript functions.
How it connects
Connects first-class functions with higher-order functions, closures, functional programming, and object-oriented programming.

Also explained in

Compare how 7 books explain first-class object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.