美文网首页
数组中求最大最小值的办法

数组中求最大最小值的办法

作者: JSONYUAN | 来源:发表于2019-03-05 14:31 被阅读0次

<script>

var arr = [11, 22, 1231, 12, 121, 21];
var arr2 = [11, 22, 99, 12, 121, 21];

// var res = Math.max.apply(null, arr);
// alert(res);
// 如果 Array 原型上没有求最大值最小值的方法,我们就给Array 拓展
//  以后可能完善该功能,如果该功能以后存在就不拓展了
if (Array.prototype.getMax == undefined) {
    Array.prototype.getMax = function () {
        // this  指向调用该函数的那个对象
        return Math.max.apply(null, this);
    };
    Array.prototype.getMin = function () {
        // this  指向调用该函数的那个对象
        return Math.min.apply(null, this);
    };
}

console.log(arr.getMax());
console.log(arr2);
console.log(arr2.getMax());

</script>

相关文章

网友评论

      本文标题:数组中求最大最小值的办法

      本文链接:https://www.haomeiwen.com/subject/ryanuqtx.html