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

<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>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容