math.min.call_最大化:使用Math.min和.max

math.min.call

Being confronted with a series of numbers and having to find the largest or smallest in the set is a common coding task. While it’s entirely possible to use a mathematical operator in JavaScript to find an answer, or even a series of if statements, the Math.max function (and it’s opposite, Math.min) are more effective and efficient.

面对一系列数字并必须在集合中找到最大或最小的数字是一项常见的编码任务。 尽管完全有可能在JavaScript中使用数学 运算符来查找答案,甚至可以找到一系列if语句 ,但Math.max函数(与之相反, Math.min )更加有效。

Unlike functions that you write for yourself, Math.max is a “built-in” function in JavaScript that you can use natively. Given a series of numbers os arguments, Math.max will always return the largest number. For example, in the console:

与您自己编写的函数不同, Math.max是JavaScript中的“内置”函数,您可以直接使用它。 给定一系列数字os参数, Math.max将始终返回最大数字。 例如,在控制台中

Math.max(5, 10, 11, 3, 22);
> 22

If none of the supplied arguments resolve to a number, the result is the annoying (and to new coders, often cryptic) Nan (Not A Number).

如果提供的参数都不解析为数字,则结果是烦人的(以及新的编码员,通常是隐秘的) Nan (非数字)。

Note that Math.max can also take variables, or object properties:

请注意, Math.max也可以采用变量或对象属性:

let orbitalPeriod = new Object();
orbitalPeriod.Mercury = 87.97,
orbitalPeriod.Venus = 224.70,
orbitalPeriod.Mars = 686.98;
Math.max(orbitalPeriod.Mercury, orbitalPeriod.Venus, orbitalPeriod.Mars)
> 686.98

One thing it can’t do is automatically find the largest value in an array. This has traditionally required looping, comparing each value and preserving the greater. For browsers that support ES6, the spread operator is far more succinct:

有一两件事不能做的是自动发现的最大的价值阵列 。 传统上,这需要循环 ,比较每个值并保留更大的值。 对于支持ES6的浏览器, spread运算符更为简洁:

let orbitalPeriods = [87.97, 224.70, 686.98],
longestOrbit = Math.max(... orbitalPeriods);

Math.max is often used to create an “either / or” result for some comparison. For example, if you were looking at screen sizes:

Math.max通常用于创建“或/或”结果以进行比较。 例如,如果您正在查看屏幕尺寸:

let screenWidth = (screen.width, 0)

The screenWidth variable, assigned via let, must resolve to a number: either the width of the screen or (if that test was unsuccessful, and does not yield a number) 0.

通过let分配的screenWidth变量必须解析为数字:屏幕的宽度或(如果该测试不成功,并且不产生数字)则为0。

达到下限 (Hitting the Lower Limit)

Math.min is the opposite: given a series of values, .min finds the smallest.

Math.min相反:给定一系列值, .min找到最小的

Math.min(0.139, 0.15, 1);
> 0.139

Math.min is often used for setting or determining boundary conditions. For example, let’s say we have a ball bouncing inside a rectangular area. The right side of the rectangle is at 500 pixels; in a collision state with the ball, we want to the right side of the ball (ball.right) to be no further than the right side of the rectangle (500).

Math.min通常用于设置或确定边界条件。 例如,假设我们有一个在矩形区域内弹跳的球。 矩形的右侧为500像素; 在与球碰撞的状态下,我们希望球的右侧( ball.right )不超过矩形( 500 )的右侧。

let collide = Math.min(ball.right,500);

Again, you can use spread to “distribute” the values in an array into the Math.min function. Given an array bugSizes, to find the smallest insect:

同样,您可以使用spread将数组中的值“分布”到Math.min函数中。 给定一个数组bugSizes ,以查找最小的昆虫:

Math.min(...bigSizes)

Alternatively (and with greater browser support), use apply to achieve the same result:

另外,(并具有更好的浏览器支持)请使用apply获得相同的结果:

Math.min.apply(null, bigSizes);

Like spread, the apply technique can also be used with Math.max.

spread一样, apply技术也可以与Math.max一起使用。

翻译自: https://thenewcode.com/1183/To-the-Max-Using-Mathmin-and-max

math.min.call

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值