JavaScript内置对象Math查询一组数中的最大值

//查找一组数据中的最大值
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);

猜你喜欢

转载自www.cnblogs.com/cuilichao/p/9320752.html
今日推荐