liveBook · Manning

Bind operator

As explained in The Joy of JavaScript

A proposed JavaScript operator syntax for binding a method or function call to a receiver, often written ::.

The bind operator is proposed JavaScript syntax, commonly written ::, for extracting a method while binding its this receiver. It combines method extraction with behavior similar to Function#bind, call, and apply. section 5.6 section 1.1

Use either operator form according to where the receiver and method appear:

  • Binary form, obj :: method, places the bound object before the method: Success.of(2)::map(x => x * 2). section 5.6
  • Unary form, ::obj.method, extracts a method already associated with an object, such as ::console.log or ::incrementor.inc. section 5.6
  • Both forms produce a bound function, avoiding explicit calls to Function#bind when callbacks or fluent transformations need a stable this value. section 5.6

Fluent transformations and callbacks

Success.of(2)::map(x => x * 2)::map(x => x ** 2) produces Success(16). For callback binding, [1, 2, 3].map(::incrementor.inc) produces [3, 4, 5], replacing incrementor.inc.bind(incrementor). section 5.6

Reactive method extraction

ReactiveExtensions exposes methods such as skip, filter, map, and reduce. Observable.of(1, 2, 3, 4) :: skip(1) :: filter(isEven) :: map(square) :: reduce(add, 0) builds a stream pipeline while controlling each method's this reference. section 9.4.6

Success.of(2)::map(x => x * 2); // Success(4)

See the minimal binary-form example for binding map to a Success value. section 5.6

How The Joy of JavaScript covers it

How deep it goes
Defines binary and unary forms with worked functor, callback, and observable examples, but defers feature enablement details to Babel.
Examples
Demonstrates Success transformations, safe division, console logging, SafeIncrementor callbacks, and Observable pipelines.
What it assumes
Assumes JavaScript methods, this binding, callbacks, and functional transformations; demonstrates the distinction through concrete examples.
What you can do after
Apply bound callbacks and fluent method transformations using proposed :: syntax when the feature is enabled.
How it connects
Connects this binding with Function.prototype.bind, method extraction, functors, monads, pipeline operators, and observables.

Related concepts

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