liveBook · Manning

Function binding

As explained in Functional Programming in JavaScript

The technique of associating a function with a specific this value or preset arguments so it can be invoked in a controlled context.

Function binding creates a new function with selected values already attached. Instead of supplying every argument each time, configure a more specific function and call it later. section 4.4

Configure a logger

const log = _.bind(logger, undefined, 'console', 'json', 'FJS Binding'); log('WARN', 'FP is too awesome!'); The bound function keeps the logger configuration and accepts the remaining value when called. section 4.4

Function binding is useful when:

  • A general function should become a reusable function for one specific configuration. section 4.4
  • A callback needs preset arguments while another argument remains available at call time. section 4.4
  • Several related operations should share configuration without repeating it. section 4.4

Binding differs from currying: it fixes arguments and returns a function that receives the remaining arguments together, rather than producing a chain of unary functions. section 4.4

How Functional Programming in JavaScript covers it

How deep it goes
Defines binding, contrasts it with currying and partial application, and demonstrates configured callbacks without detailing binding internals.
Examples
Demonstrates a bound logger, partially configured string methods, and scheduled callbacks.
What it assumes
Assumes familiarity with JavaScript functions and introduces binding alongside partial application and currying.
What you can do after
Apply it via Lodash _.bind or JavaScript Function.prototype.bind to create configured functions.
How it connects
Connects function binding to partial application, currying, closures, callbacks, and functional composition.

Also explained in

Compare how 2 books explain function binding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.