Somehow I didn’t know that you can use bind as a built in way to do partial function application in JavaScript.

Somehow I didn’t know that you can use bind as a built in way to do partial function application in JavaScript.

var add = function (a, b) {

return a + b;

};

var add5 = add.bind(null, 5);

add5(10) === 15;

The first arg is commonly used to bind the function “this” variable. What I didn’t realise was that subsequent args get prepended as arguments to the bound function.

3 thoughts on “Somehow I didn’t know that you can use bind as a built in way to do partial function application in JavaScript.

  1. The first argument binds to the internal “this” variable so it can be ignored in this case. It’s only relevant for object methods, not functions.

    It’s the second and later args that can be used for partial application. So ignore the first argument.

    Like

  2. I use this to great effect to generate test functions for Nightwatch et al, where the framework itself demands a particular, fixed, function signature that doesn’t otherwise allow me the parameterisation I need for the coverage I want.

    Like

Leave a reply to Paul Lambros Cancel reply