0

I would like to write curring function for object methods. I want to make this possible:

Function.prototype.curry = function (){
  var originalFunction = this;
  var args = ...; // here goes logic embracing arguments
  var bind = ???; //how to get reference to someObject ???
  return function(){
    return originalFunction.apply(bind, args);
  }
}

var someObject = {
  doSomething : function (param1, param2, param3){
    //do something with params
    return param1 + ' ' + param2 + ' ' + param3;
  }
}

someObject.doSomethingCurried = someObject.doSomething.curry('param1 value', 'param2 value');

//I want to be able to do:
someObject.doSomethingCurried('param3 value')'
Marecky
  • 1,924
  • 2
  • 25
  • 39

2 Answers2

1

There are some tricks, but in fact you should just pass context as a first argument, like native bind.

Qwertiy
  • 19,681
  • 15
  • 61
  • 128
0

// Code goes here

Function.prototype.curry = function (context,arg){
  var originalFunction = this;
  var args = Array.prototype.slice.call(arguments,1) ; // here goes logic embracing arguments
  var bind = context; //how to get reference to someObject ???
  return function(){
    return originalFunction.apply(bind, args.concat(Array.prototype.slice.call(arguments)));
  }
}

var someObject = {
  myObj:"Myobj",
  doSomething : function (param1, param2, param3){
    console.log(this);
    //do something with params
    return param1 + ' ' + param2 + ' ' + param3;
  }
}

someObject.doSomethingCurried = someObject.doSomething.curry(someObject,'param1 value', 'param2 value');

//I want to be able to do:
console.log(someObject.doSomethingCurried('param3 value'));
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30