var a=[1,2,3,5]; alert(Math.max.apply(Math, a));//最大值 alert(Math.min.apply(Math, a));//最小值
在此處,把Math對象做爲apply()的第一個參數,從面正確指定執行對象this值。而後,能夠將任何數組做爲第二個參數傳遞,由於apply第二個參數,原本就要傳遞數組數組
根據《JS高級程序設計》,能夠利用Math.random()從某個整數範圍內隨機選擇一個值:app
值 = Math.floor(Math.random() * 可能值的總數 + 第一個可能值);dom
由於Math.random()返回是大於等於0小於1的隨機數;例如想獲取一個1-10之間的隨機數,代碼以下:this
var num = Math.floor(Math.random() * 10 +1 );
要獲取2-9之間的隨機數,代碼以下:spa
1 var num = Math.floor(Math.random() * 9 + 2 );
從2到9一共9個數,所以總數爲9,第一個可能的值爲2,所以要加1一下保證在2-9之間設計