liveBook · Manning

Function

As explained in Secrets of the JavaScript Ninja, Third Edition

A callable unit of code that can receive arguments and return a value.

A JavaScript function is callable code, but it is also an object. You can store one in a variable or property, pass it to another function, return it, and attach properties to it. That combination makes functions useful for organizing behavior as well as performing it. section 5.2.1

Choose a definition form to suit where the function belongs:

  • A declaration, such as `function answer() { return 42; }`, has a name and can be called before its definition appears. section 5.1
  • An expression can assign a function to a variable; an ES2015 arrow function offers shorter syntax and handles `this` differently. section 5.1
  • Object literals and classes can define methods. The `Function` constructor can build code from strings, but interpreting strings as code carries security concerns. section 5.1

Passing behavior as an argument

`function call(callback) { return callback(); }` receives a function, then invokes it. A parameter is the name in the definition (`callback`); an argument is the value supplied at the call. Such a passed function is a callback: other code decides when to invoke it. Event handlers and array sorting put the same idea to practical use. section 5.2.2 section 5.6

Because functions are objects, a `Set` can hold unique callback references. Adding the same function reference twice stores it once; creating two functions with identical bodies produces two distinct references. This matters when one event should not invoke the same registered callback twice. section 5.3.1

Listing 5.5 Storing a collection of unique functions
let count = 0;

const increment = () => {
  count++;
};

const funcSet = new Set();  #A
funcSet.add(increment);     #B
funcSet.add(increment);     #B

funcSet.forEach((func) => func());                #C
console.assert(count === 1,                       #D
  "The function was only added to the set once"); #D

The listing adds one `increment` function twice, then invokes the set’s contents. The count changes only once. section 5.3.1

Function properties can carry metadata: a React component’s `displayName`, for example, helps identify it in React DevTools when minification loses its declared name. Avoid attaching private working data casually, since any code with access to the function can read or change that data. section 5.3.2

A call need not supply exactly one argument per parameter: missing parameters evaluate as `undefined`, and extra arguments are not bound to parameter names. Default and rest parameters provide ways to handle those cases. TypeScript can check typed calls before execution. section 5.6

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Works through definition forms, callback invocation, reference equality, and metadata; defers detailed treatment of generators and class fields.
Examples
Demonstrates a passed callback, event handling, array sorting, unique callbacks in a Set, and React display metadata.
What it assumes
Assumes basic object fundamentals and develops JavaScript’s first-class-function behavior from that foundation.
What you can do after
Build it from scratch: define functions, pass callbacks, manage unique callback references, and distinguish parameters from arguments.
How it connects
Connects functions to objects, callbacks, Sets, memoization, rest parameters, and default parameters.

Also explained in

Compare how 26 books explain function →

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.