Method Chaining & Currying javascript
One of major interview question , that is asked in javascript is regarding currying. Currying definition as mentioned in wikipedia is
currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument.
What we are going to achieve in the article ?
We will be writing a simple curry function for number addition, which will allow to chain mutliple calls with varying arguments.
For enabling currying , we need to transform our simple add function. Our simple add function will look like this
Now as we are done with basic add functionality. we have to write our add curry function in such a way that it concats the arguments passed to consecutive function calls in chain and result a function.
Why a function ? Because there is no way of determining when the chain is completed. In the end we will be invoking the function to get our final desired result.
Now using addCurry will be something like this
Currying is special way of making your methods more resilient and usable. It can be used at places where number of arguments are not known statically.
Here is the complete running code
Over to you guys. Let me know whether you liked the article by hitting applause or comments. If you find any problems with the article please let me know. Thanks everyone !!