2

I'm playing around with Function.prototype as a learning exercise and was trying to make a generic curry method that could be used like this:

// Any old function
var myFn = function(arg1, arg2) {
    console.log(arg1, arg2);
};

// Curry some parameters and return a new function
var myFnCurry = myFn.curry("A");

// Call the curried function, passing in the remaining parameters
myFnCurry("B"); // Outputs: A B

It's fairly straight forward to implement this feature as a function rather than a method using the following approach:

var curry = function(fn) {
    var slice = Array.prototype.slice,
        args = slice.call(arguments, 1);
    return function() {
        fn.apply(this, args.concat(slice.call(arguments)));
    };
};

// Example
var myFnCurry = curry(myFn, "A");
myFnCurry("B"); // Outputs: A B

However, I really wanted to be able to utilise the Function prototype, like this:

Function.prototype.curry = function() {
    var slice = Array.prototype.slice,
        args = slice.call(arguments);
    return function() {
        // If we call curry like this: myFn.curry()
        // how can we get a reference myFn here?
        ???.apply(this, args.concat(slice.call(arguments)));
    };
};

I'm not sure how to get a reference to the myFn function (denoted by the ??? above) that the curry method is being called from.

Is there a way to access the parent function in this circumstance?

Cheers,

Ian

1 Answer 1

2

You're calling curry in a context of your function object (myFn.curry), and therefore inside of curry this refers to that function. But your inner function will be called in the context of global object, that's why you need to store the reference to the outer function in a closure.

Function.prototype.curry = function() {
  var self = this, args = [].slice.call(arguments)
  return function() {
    return self.apply(this, args.concat([].slice.call(arguments)));
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I had explored the 'this' context issue and assigning a reference to it, but I think I wasn't seeing the wood for the trees in the end and had the assignment in the inner function instead of the outer. Thanks so much for pointing me in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.