Math.min()
Math.min() 静态方法返回作为输入参数给出的数字中的最小者,如果没有参数,则返回 Infinity。
¥The Math.min() static method returns the smallest of the numbers given as input parameters, or Infinity if there are no parameters.
Try it
语法
参数
返回值
描述
¥Description
因为 min() 是 Math 的静态方法,所以你始终将其用作 Math.min(),而不是用作你创建的 Math 对象的方法(Math 不是构造函数)。
¥Because min() is a static method of Math, you always use it as Math.min(), rather than as a method of a Math object you created (Math is not a constructor).
Math.min.length 是 2,这微弱地表明它被设计为处理至少两个参数。
¥Math.min.length is 2, which weakly signals that it's designed to handle at least two parameters.
示例
使用 Math.min()
使用 Math.min() 剪切值
¥Clipping a value with Math.min()
Math.min() 通常用于裁剪一个值,使其始终小于或等于边界。例如,这个
¥Math.min() is often used to clip a value so that it is always less than or
equal to a boundary. For instance, this
let x = f(foo);
if (x > boundary) {
x = boundary;
}
可以写成这样
¥may be written as this
const x = Math.min(f(foo), boundary);
Math.max() 可以以类似的方式使用来剪辑另一端的值。
¥Math.max() can be used in a similar way to clip a value at the other end.
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-math.min |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also