Why can't I write [1,2,3].reduce(Math.max) in Javascript? [duplicate]
Possible Duplicate:开发者_如何学运维
How to use Math.max, etc. as higher-order functions
Using Mozilla's Javascript 1.6 array 'extension' functions (map, reduce, filter etc.), why is it that the following works as expected:
var max = [1,2,3].reduce(function(a,b) { return Math.max(a,b); });
But the following does not work (it produces NaN):
var max2 = [1,2,3].reduce(Math.max);
Is it because Math.max is a variadic function?
Math.max
does not know what to do with all the extra variables function(previousValue, currentValue, index, array)
mainly that array at the end.
[].reduce.call([1,2,3,6],function(a,b) { return Math.max(a,b); });
This works and uses .call
精彩评论