What the Hell is currying?
If you’ve ever come across the concept of currying in JavaScript, you probably had a similar reaction to mine which was almost a total rejection. Not only do you have to understand .bind() to understand currying (already a tough obstacle on the route to being a pro), but the term ‘currying’ easily associates itself with ‘curry’, the delicious Indian spice. (I hope I’m not the only one.)
Once I was able to fully understand .bind(), I finally got over my self-inflicted blindness and decided to look at currying square in the face and tackle it. Thanks to the help of Hack Reactor’s curriculum and fantastic staff, I did just that.
Before you begin reading this, please be sure to read my previous post, Understanding .bind(), .call(), and .apply().
First off, what is currying? The concept of currying came from a computer scientist named Haskell CURRY. That’s right, CURRY. And that’s where the name currying comes from. He developed this concept. Which handles the first barrier of associating this with food.
Currying is when you bind a function to an object using the .bind() method, but at the time of the binding you pass in only a partial list of the arguments needed for the function. Take a look at the following:
____________________________________
var helloObject = { hello: ‘hello’ };
var helloWorld = function (word1, word2, word3) {
return this.hello + word1 + word2 + word3 +‘ world!’;
};
var crazyWorld = helloWorld.bind(helloObject, ‘ crazy’);
____________________________________
Notice that I I bound helloObject to the function helloWorld with ONLY ONE ARGUMENT. This created a new function named crazyWorld. Later, when I call crazyWorld(), I will pass in my other arguments in at the time. However, my first argument, ‘crazy,’ will always be understood to be the first argument (hence the name of the function, crazyWorld).
And this is currying. The only reason why I would ever want to do this is if I wanted to always express that the world is crazy, but may change in other ways in the future.