1. 程式人生 > >JavaScript內置對象Math查詢一組數中的最大值

JavaScript內置對象Math查詢一組數中的最大值

最大值 定義 func 內置對象 .get scrip new i++ 查找

//查找一組數據中的最大值
var result = Math.max(10, 20, 39, 40);
alert(result);

//自定義一個對象,實現系統方法max的方法
function MyMath() {
//添加了一個方法
this.getMax = function () {
var max = arguments[0];
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
};
}
//實例對象
var mt = new MyMath();
var result1 = mt.getMax(1, 3, 6, 9, 2, 6, 3);
alert(result1);

JavaScript內置對象Math查詢一組數中的最大值