我想了解部分函数。我发现这个例子(http://blog.thesoftwarecraft.com/2013/05/partial-functions-in-javascript.html),我无法完全理解。javascript部分函数,参数
function partial(f) {
console.log(f) // tip(p,check)
var args = Array.prototype.slice.call(arguments, 1); //0.2
var test_args = Array.prototype.slice.call(arguments);
console.warn(test_args) // [tip(p,check), 0.2]
return function() {
console.warn(arguments) //[120, 0, [120, 90, 180]] [90, 0, [120, 90, 180]] ...
//where do these arguments come from? why don't appear at test_args?
var other_args = Array.prototype.slice.call(arguments); //[120, 0, [120, 90, 180]] [90, 0, [120, 90, 180]] ...
console.log(args.concat(other_args)) // added percentage to array[0.2, 120, 0, [120, 90, 180]]
return f.apply(null, args.concat(other_args)); //we execute tip with all the arguments (only 2 first will be used)
}
}
function tip(percentage, check) {
return check * percentage
}
[120, 90, 180].map(partial(tip, 0.2)); //[24, 18, 36]
根据你的问题,你需要阅读['Array#map'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) ,而不是部分功能。 (并仔细检查你在评论中写下的值,'[90,0,[120'应该是'[90,1,[120'。] – DCoder
[arguments](https://developer.mozilla.org)/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments),[partial application](http://benalman.com/news/2012/09/partial-application-in-javascript/#partial-application) – Andreas