在JavaScript忍者的秘密上工作时,我看到了curry函数。
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)));
};
};然后,我试图通过运行split函数(它通过Function.prototype.curry定义继承它)来使用它。
var splitIt = String.prototype.split.curry(/,\s*/); // split string into array
var results = splitIt("Mugan, Jin, Fuu");
console.log("results", results); 但是[]打印出了结果。为什么?
http://jsfiddle.net/2KCP8/
发布于 2013-12-27 16:10:59
您的"splitIt“函数仍然期望this引用要拆分的字符串。你还没安排好这件事。
试一试
var results = splitIt.call("Mugan, Jin, Fuu");https://stackoverflow.com/questions/20803777
复制相似问题