liveBook · Manning

First-class object

As explained in Secrets of the JavaScript Ninja, Third Edition

A value that can be assigned, stored, passed as an argument, and returned from a function.

In JavaScript, functions are first-class objects: they can be referenced by variables, created as literals, and passed to or returned from other functions. section 5.0 section 5.1.1

This makes functions usable in several places where other values appear:

  • Assign a function expression to a variable or property. section 5.1.1
  • Pass a function as an argument or return one from another function. section 5.1.1
  • Use function methods such as call and apply to choose an invocation context; apply takes arguments in an array, while call takes them individually. section 6.2.4

A function passed as an argument

const myFunc = function(){}; myFunc(function() { return function(){}; }); section 5.1.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains function expressions and their placements, while deferring scope and identifier-resolution questions.
Examples
Uses functions assigned to variables, passed as arguments, returned from functions, and invoked with call or apply.
What it assumes
Assumes working knowledge of JavaScript object fundamentals and does not target rank beginners.
What you can do after
Apply functions as arguments or return values, and set their invocation context with call or apply.
How it connects
Connects first-class functions to function expressions, object properties, and invocation context through call and apply.

Also explained in

Compare how 8 books explain first-class object →

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.