liveBook · Manning

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

What you will learn in chapter 3 of Secrets of the JavaScript Ninja, Second Edition

The introduction explains why functions are central to JavaScript and establishes that they are first-class objects.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why functions are central to JavaScript and establishes that they are first-class objects.

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

3.1 What’s with the functional difference?

This section explains why functions matter in JavaScript and introduces their object-like capabilities.

Key terms: functions, modular units of execution, function invocation, global JavaScript code, objects, first-class objects, arguments, properties

3.1 Do you know?

This section poses review questions about callbacks, arrow functions, and default parameter values without explaining their answers.

3.1.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, arguments, functional programming, composing functions, imperative programming

3.1.2 Callback functions

This section explains how functions serve as callbacks, from direct invocation and browser events to comparison functions used by array sorting.

Key terms: callback, event handler, asynchronously, getText, useless, callback parameter, callback(), expression, global namespace, first-class objects

3.2 Fun with functions as objects

The section introduces functions as objects that can have properties and gives two high-level uses: managing related functions and remembering computed values.

Key terms: functions, properties, objects, collection, callbacks, memoization, previously computed values, subsequent invocations

3.2.1 Storing functions

This section shows how to store unique callback functions by marking each function with an id property and caching it under that id.

Key terms: callbacks, unique functions, duplicates, store, add(), id property, nextId, cache

3.2.2 Self-memoizing functions

This section explains how a function can cache its computed results on itself, reuse them for repeated inputs, and trade memory for speed.

Key terms: memoization, previously computed values, function arguments, isPrime, answers, cache, property key, performance, memory, business logic

3.3 Defining functions

Introduces functions as values and distinguishes four ways to define them at a high level.

Key terms: function literal, function value, first-class objects, function declarations, function expressions, arrow functions, function constructors, generator functions

3.3.1 Function declarations and function expressions

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

Key terms: function declaration, function expression, function body, function name, parameter name, immediately invoked function expression, IIFE, immediate function, function call parentheses, JavaScript parser

3.3.2 Arrow functions

This section introduces arrow functions as a concise form of function expression and explains their parameter syntax and two ways of producing a return value.

Key terms: arrow functions, function expressions, callback, parameter names, fat-arrow operator, =>, expression, block of code, return statement, undefined

3.4 Arguments and function parameters

This section distinguishes function parameters from arguments and explains how JavaScript assigns arguments when a call supplies more or fewer values than the function defines parameters for.

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

3.4.1 Rest parameters

Explains how ES6 rest parameters collect extra function arguments into an array and illustrates their use in a calculation.

Key terms: rest parameters, ellipsis (...), SyntaxError: parameter after rest parameter, multiMax, first, remainingNumbers, descending order

3.4.2 Default parameters

This section explains why ES6 default parameters are useful, how omitted arguments receive defaults, and how default values are evaluated.

Key terms: default parameters, matching argument, default value, function overloading, typeof operator, undefined, evaluated on each function call, previous parameters

3.5 Summary

3.6 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 2: Chapter 2. Building the page at runtime Chapter 4: Chapter 4. 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.