.bind it's more than just this.

Adam Crockett 🌀 - May 22 '20 - - Dev Community

You might know about bind, it takes a function and binds the this to any object you like. At least that's the common usage. Bind has another hidden super power I didn't know about for years.

function add(a, b){
  return a + b;
}
// With me so far?

const addToFive = add.bind(null, 5);

addToFive(20); // 25

// Okay let's head back to the article
Enter fullscreen mode Exit fullscreen mode

We just, for lack of a better word preloaded one of the arguments of add using the second arg of the bind method, the null just means we bound this to a null object, because null is an object.

The effect is this, we can call a function with a predetermined argument.

Ps
Note I'm still working on the parallel universe series I can't wait to show you.

You don't have to use bind just so you know, you can use a functional technique called currying, see the comments for details.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .