js取数组最大值之Math.max、Math.max.apply
js取数组最大值之Math.max、Math.max.apply
- Math.max
- Math.max.apply
- apply()
- 第一个参数为什么可以是null
- 最小值同理
Math.max
Math.max(n1,n2,n3,…,nX)
支持传递多个参数,带有较大的值的那个数
Math.max(2,5,3,6,2,4,2,15,9,6,0,1)
Math.max.apply
apply()
- 语法:fun.apply(thisArg, [argsArray])
thisArg:在fun函数运行时指定的 this 值 ,可以为null,就是不设置指向
argsArray:传递的值,必须包含在数组里面
第一个参数为什么可以是null
这块在调用的时候第一个参数给了一个null,这个是因为没有对象去调用这个方法,
我只需要用这个方法帮我运算,得到返回的结果就行,.所以直接传递了一个null过去
Math.max.apply(null,arr)
支持传递数组
const arr = [1, 66, 3, 99, 4];
Math.max.apply(Math, arr)