From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/global_objects/math/max/index.html | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/math/max/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/math/max/index.html') diff --git a/files/zh-cn/web/javascript/reference/global_objects/math/max/index.html b/files/zh-cn/web/javascript/reference/global_objects/math/max/index.html new file mode 100644 index 0000000000..593776318e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/math/max/index.html @@ -0,0 +1,99 @@ +--- +title: Math.max() +slug: Web/JavaScript/Reference/Global_Objects/Math/max +tags: + - JavaScript + - Math +translation_of: Web/JavaScript/Reference/Global_Objects/Math/max +--- +
{{JSRef}}
+ +

Math.max() 函数返回一组数中的最大值。

+ +

{{EmbedInteractiveExample("pages/js/math-max.html")}}

+ +

语法

+ +
Math.max(value1[,value2, ...]) 
+ +

参数

+ +
+
value1, value2, ...
+
一组数值
+
+ +

返回值

+ +

返回给定的一组数字中的最大值。如果给定的参数中至少有一个参数无法被转换成数字,则会返回 {{jsxref("NaN")}}。

+ +

描述

+ +

由于 maxMath 的静态方法,所以应该像这样使用:Math.max(),而不是创建的 Math 实例的方法(Math 不是构造函数)。

+ +

如果没有参数,则结果为 - {{jsxref("Infinity")}}。

+ +

如果有任一参数不能被转换为数值,则结果为 {{jsxref("NaN")}}。

+ +

示例

+ +

使用 Math.max()

+ +
Math.max(10, 20);   //  20
+Math.max(-10, -20); // -10
+Math.max(-10, 20);  //  20
+ +

下面的方法使用 {{jsxref("Global_Objects/Function/apply", "apply")}} 方法寻找一个数值数组中的最大元素。getMaxOfArray([1,2,3]) 等价于 Math.max(1, 2, 3),但是你可以使用 getMaxOfArray ()作用于任意长度的数组上。

+ +
function getMaxOfArray(numArray) {
+    return Math.max.apply(null, numArray);
+}
+
+ +

或者通过使用最新的扩展语句{{jsxref("Operators/Spread_operator", "spread operator")}},获得数组中的最大值变得更容易。

+ +
var arr = [1, 2, 3];
+var max = Math.max(...arr);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.0.
{{SpecName('ES5.1', '#sec-15.8.2.11', 'Math.max')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-math.max', 'Math.max')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-math.max', 'Math.max')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ +

{{Compat("javascript.builtins.Math.max")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf