JavaScript: Currying in JavaScript

25 / Sep / 2012 by Kushal Likhi 0 comments

Hi,

 

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:
[js]
//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"

[/js]

 

 

Hope it Helps!
Kushal Likhi

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *