Reputation: 41909
Working on Secrets of the JavaScript Ninja, I saw the curry
function.
Function.prototype.curry = function() {
var fn = this, args = Array.prototype.slice.call(arguments);
return function() {
return fn.apply(this, args.concat(
Array.prototype.slice.call(arguments)));
};
};
Then I tried to use it by currying the split
function (which inherited it through the Function.prototype.curry
definition.
var splitIt = String.prototype.split.curry(/,\s*/); // split string into array
var results = splitIt("Mugan, Jin, Fuu");
console.log("results", results);
But []
prints out for the results. Why?
Upvotes: 4
Views: 764
Reputation: 413709
Your "splitIt" function still expects that this
will refer to the string to be split. You haven't arranged for that to be the case here.
Try
var results = splitIt.call("Mugan, Jin, Fuu");
Upvotes: 3