liveBook · Manning

First-class object

As explained in Secrets of the JavaScript Ninja, Second Edition

A value that can be assigned, passed as an argument, returned, and otherwise handled like an ordinary value.

JavaScript may look familiar if you know C# or Java, but its functions deserve a different mental model: a function is an object you can move around, not just a named block of code. “First-class” means it can be handled like other values. It also has one extra capability: you can invoke it. section 1.1 section 3.1.1

A function can be:

  • Created with a literal and assigned to a variable, an array entry, or an object property. section 3.1.1
  • Passed to another function as an argument, or returned from a function. The receiving function can invoke a passed-in function later as a callback. section 3.1.1
  • Given dynamically assigned properties, just like other objects. section 3.1.1

Define it where you need it

Compare `var a = 3; myFunction(4);` with `var a = function() {}; myFunction(function(){});`. A function expression can occupy the same assignment or argument position as another value. Unlike a function declaration, it is part of another statement; placing it at the point of use can make code easier to follow. section 3.3.1

Listing 3.5. Function declarations and function expressions
function myFunctionDeclaration(){            #A
  function innerFunction() {}                #B
}

var myFunc = function(){};                   #C
myFunc(function(){                           #D
  return function(){};                       #E
});

(function namedFunctionExpression () {        #F
})();                                         #F

+function(){}();                              #G 
-function(){}();                              #G
!function(){}();                              #G
~function(){}();                              #G

Compare the standalone and nested declarations with expressions assigned, passed as arguments, and returned as values. section 3.3.1

This flexibility supports callbacks and is a starting point for functional programming: composing functions to solve problems. That broader style, along with closure and scope mechanics, is a separate topic. section 3.1.1 section 1.1

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Explains assignable, passable, returnable, and invokable functions with code; defers detailed closure and scope mechanics.
Examples
Compares number and function literals in assignments and arguments, then contrasts function declarations with expressions.
What it assumes
Assumes basic JavaScript object knowledge while introducing first-class function behavior; postpones tricky scope and identifier resolution questions.
What you can do after
Practice/perform it by placing function expressions in assignments, arguments, and return positions.
How it connects
Connects first-class functions to function expressions, callbacks, functional programming, and closures.

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 Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.