开发者

Why can't I write [1,2,3].reduce(Math.max) in Javascript? [duplicate]

This question already has answers here: Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜