var arr = [7,2,0,-3,5];
1.apply()應用某一對象的一個方法,用另一個對象替換當前對象
var max = Math.max.apply(null,arr); console.log(max)
由於max()里面參數不能為數組,所以借助apply(funtion,args)方法調用Math.max(),function為要調用的方法,args是數組對象,當function為null時,默認為上文,即相當於apply(Math.max,arr)
2.call()調用一個對象的一個方法,以另一個對象替換當前對象
var max1 = Math.max.call(null,7,2,0,-3,5) console.log(max1)
call()與apply()類似,區別是傳入參數的方式不同,apply()參數是一個對象和一個數組類型的對象,call()參數是一個對象和參數列表
3.sort()+reverse()
//sort()排序默認為升序,reverse()將數組掉個 var max3 = arr.sort().reverse()[0]; console.log(max3)
4.sort()
//b-a從大到小,a-b從小到大 var max2 = arr.sort(function(a,b){ return b-a; })[0]; console.log(max2)