PAPP

Jamie Skinner
2 min readApr 28, 2015

PAPP, or Partial Application, is a great way to reuse your code so you can write less and do more. Let me run through the basics.

If you have used the bind( ) function function, then you may be familiar with something like this:

var add = function(a,b){
return a + b;
}
add(1,2) //=> 3var add5 = add.bind(this, 5);add5(6) //=> 11

If you aren’t familiar, here is what is going on: the bind function returns a new function, where the first argument is set to the returned functions this value. Every call of the returned function will then be applied to the passed in this. Every consecutive argument will be bound to its cooresponding argument in the original function.

Confused? What is happening here is we define an add function that takes two arguments and simply returns their sum.

We then use the bind function function to create a new function which we have called add5. As the name suggests, all we want to achieve with the add5 function is to add 5 to any number.

Well… We’ve already written an add function, so we just need to make a new function where one argument is always 5. For this we use bind, and pass in this as the first argument and 5 as the second. This makes the context of the returned function (ie. add5) the same as the add function. It also permanently binds the number 5 to the a argument in the original add function.

Similarly: if we wanted to make a giveMe11 function, all we would have to do is this:

var giveMe11 = add5.bind(this,6);

Now that you now the context, here is PAPP!

With this, you can achieve the same results but for any function! The only difference here is we use call instead of bind. The only difference is call is the second argument is an array of arguments you want to pass, rather than the argument itself.

And thats it! Try it out.

--

--