我们再来聊聊Function.apply() 在提升程序性能方面的技巧。
我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值。
比如
alert(Math.max(5,8)) //8
alert(Math.max(5,7,9,3,1,6)) //9
但是在很多情况下,我们需要找出数组中最大的元素。
var arr=[5,7,9,1]
alert(Math.max(arr)) // 这样却是不行的。一定要这样写
function getMax(arr){
var arrLen=arr.length;
for(var i=0,ret=arr[0];i<arrLen;i++){
ret=Math.max(ret,arr[i]); ......
阅读全文