Functions and higher-order code
This part of the JavaScript course follows a call: it binds arguments to parameters by position, a default or a rest parameter changes what is bound, and an options object replaces a long list with names. Then functions become values: a pure function is tested by its return value, a callback is code run later, and map, filter and reduce are written by hand before they are used. Currying, composition and memoization build one function out of others, recursion, generators and the iterator protocol supply the lazy forms, and the part ends with a validator built from these pieces.
4.1
Parameters and arguments: what a call passes
parameters bound by position, a missing argument as undefined, an extra one dropped, the arguments object of an old function, a rest parameter that collects an array
Open
4.2
Default parameters: when they are evaluated
a default for an undefined argument, the expression evaluated per call, a default that reads an earlier parameter, null and the temporal dead zone of a later one
Open
4.3
Options objects: arguments by name
the long positional list, one object destructured in the parameter list with defaults, an omitted object, unknown keys and the rest of them, a required option that throws
Open
4.4
Pure functions: same input, same output
determinism against a counter of the module, a call that changes its argument, a table of test cases with no setup, an impure shell around a pure core
Open
4.5
Functions as values: store, pass, return
a function in a binding, an array and an object, a function passed as an argument, a factory that returns one, an object of functions as a dispatch table
Open
4.6
Callbacks: passing code to be run later
a callback called once per item, the error-first convention with an early return, the nesting of three dependent steps, a retry that calls itself
Open
4.7
map, filter and reduce written by hand
each method as a for loop, the value, index and array of the callback, the accumulator and the missing initial value, the chain that reads in order
Open
4.8
Currying and partial application
one argument at a time through closures, fixed arguments with bind and with a wrapper, a curry built on fn.length, the arity trap of a default or rest parameter
Open
4.9
Composition: pipe and compose
a pipeline as a reduce over functions, compose in the other direction, an order that changes the result, a pipeline built at run time from an options object
Open
4.10
Memoization: a cache with Map
a Map from arguments to results, a memoize wrapper keyed on the argument list, the functions that must not be cached, an object key and a stale entry
Open
4.11
Recursion: the base case and the call stack
a function that calls itself, the frame per level, the RangeError of a missing base case, a walk over a tree of nodes
Open
4.12
Generators: function*, yield and next
yield and the value and done fields, for...of and the spread over a lazy sequence, a value passed into next, an early return and yield* delegation
Open
4.13
Iterators and iterables: the for-of protocol
an iterator written by hand, Symbol.iterator and the four consumers, an iterable against a used-up iterator, a class that yields its values
Open
4.14
Putting functions together: a validator library
rules as factories that return null or a message, a schema checked with an options object, a pure normalise step, the report counted with map, filter and reduce
Open