April 9, 2020
Estimated Post Reading Time ~

JavaScript: Currying in JavaScript

Currying is a very simple and useful concept in JavaScript. Here we can set the context of the function to any object or variable.
So let us study this concept using the practical examples and code as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Let us first create a function "curry"
function curry(thisObject, func){
    return function(){
          return func.apply(thisObject, arguments);
   };
}
 
//Lets make a function in which we have delegated the this arg. Such that this function is executed in the context of some other object.
var myCurriedFunction = curry("This is a string object, it will be the 'this' object", function(){
    alert(this);
});
 
//Now lets call the function:
myCurriedFunction(); //will alert "This is a string object, it will be the 'this' object"


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.