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