liveBook · Manning

Chapter 5: First-class functions for the novice: definitions and arguments

What you will learn in chapter 5 of Secrets of the JavaScript Ninja, Third Edition

The chapter introduction explains why functions are central to JavaScript and introduces their status as first-class objects.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why functions are central to JavaScript and introduces their status as first-class objects.

Key terms: functions, functional language, first-class objects, first-class citizens, function parameters

5.1 Defining functions

Introduces the main syntaxes for defining JavaScript functions and briefly identifies other forms and a security caution.

Key terms: function declaration, function expression, function keyword, arrow function, =>, this keyword, method definition, object literal, class field, generator

5.1.1 Function declarations and function expressions

This section distinguishes function declarations from function expressions and explains how function expressions can be invoked immediately.

Key terms: function declaration, function keyword, parameter names, function body, function expression, argument, return value, immediately invoked function expression, IIFE, immediate function

5.1.2 Arrow functions

This section explains arrow-function syntax and return behavior, with a brief sidebar on using console.assert to check examples.

Key terms: arrow function, arrow function operator, fat-arrow operator, parameters, expression, block of code, return statement, undefined, console.assert, truthy

5.2 The versatility of JavaScript functions

The section shows that JavaScript functions can be used in almost the same ways as objects.

Key terms: functions, objects, properties, arguments

5.2.1 Functions as first-class objects

This section explains what makes JavaScript functions first-class objects, how that enables callbacks, and how it relates to functional programming.

Key terms: first-class objects, functions, invokable, callback function, argument, functional programming, composing functions, imperative programming

5.2.2 Callback functions

This section explains how functions serve as callbacks, then uses event handling and array sorting to show when other code calls them.

Key terms: callback, callback parameter, event handler, function, expression, module namespace, sort, comparator, default comparator, localeCompare

5.3 Fun with functions

Introduces three ways JavaScript functions can be used and the benefit of each.

Key terms: collection, callbacks, metadata, minified code, memoization

5.3.1 Storing functions in a collection

This section shows how to keep a collection of callback functions unique by using a Set, and explains what counts as the same function.

Key terms: callbacks, duplicates, array, Set, reference equality, functions are objects

5.3.2 Attaching metadata to functions

This section explains when attaching data to a JavaScript function is useful, when it weakens encapsulation, and why React uses a function’s displayName for debugging.

Key terms: functions, objects, metadata, encapsulation, module’s scope, displayName, name property, React DevTools, minified

5.3.3 Memoizing functions

This section explains how memoization reuses results of expensive function calls and why a reusable wrapper can hold the cache.

Key terms: memoization, function arguments, previously computed values, wrapper function, encapsulation, isPrime, single argument, memoize-one, memory overhead

5.4 Arguments and function parameters

This section distinguishes function parameters from arguments and explains how JavaScript assigns arguments to parameters, including when their counts differ.

Key terms: parameter, argument, function definition, invocation, arguments, parameters, assigned, order, excess arguments, undefined

5.4.1 Rest parameters

This section explains how a rest parameter collects remaining function arguments into an array and shows its use in a multiplication example.

Key terms: rest parameter, ellipsis (...), remainingNumbers, SyntaxError: parameter after rest parameter

5.4.2 Default parameters

This section explains how JavaScript default parameters supply omitted arguments, what values they can use, and when they make functions easier to use.

Key terms: default parameters, matching argument, default value, primitive values, objects, arrays, functions, previous parameters, sensible defaults, code readability

5.5 Declaring functions in TypeScript

This introduction explains why JavaScript function arguments can require runtime checks and how missing types can allow errors to spread.

Key terms: arguments, type check, runtime, explicit typing, implicitly convert, null

5.5.1 Defining parameter types

This section explains how TypeScript checks function parameter types, including rest and default parameters, without adding runtime checks.

Key terms: parameter, type annotation, TypeScript compiler, runtime type check, rest parameters, number[], remainingNumbers, default parameters, infer, action

5.5.2 Defining return types in TypeScript

This section explains how TypeScript infers function return types and why developers may annotate them explicitly.

Key terms: return type, infer, union, type annotations, explicit return type, type annotation, type checker

5.5.3 Function overloading

This section explains how TypeScript function overloads describe permitted combinations of argument types without creating separate runtime implementations.

Key terms: function overloading, overload signatures, type checker, as keyword, function overloads, function keyword, arrow functions, type annotation

5.5.4 Documenting functions with TSDoc

This section explains how TypeScript type annotations and TSDoc comments help readers and editors understand functions, including functions supplied by packages.

Key terms: type annotations, TypeScript-aware editor, TSDoc, @param, @returns, .d.ts files, TSDoc comments, TypeScript types

5.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Building web applications Chapter 6: Functions for the journeyman: understanding function invocation →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.