aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/math/min/index.html
blob: a5a5a6defc1709299891604e07f5c44eb4cc8ef0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
title: Math.min()
slug: Web/JavaScript/Reference/Global_Objects/Math/min
tags:
  - JavaScript
  - Math
  - Math.min
  - 参考
  - 方法
translation_of: Web/JavaScript/Reference/Global_Objects/Math/min
---
<div>{{JSRef}}</div>

<p><code><strong>Math.min()</strong></code> 返回零个或更多个数值的最小值。</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox"><code>Math.min([<em>value1</em>[,<em>value2</em>, ...]]) </code></pre>

<h3 id="Parameters" name="Parameters">参数</h3>

<dl>
 <dt><code>value1, value2, ...</code></dt>
 <dd>一组数值</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>给定数值中最小的数。如果任一参数不能转换为数值,则返回{{jsxref("NaN")}}</p>

<h2 id="Description" name="Description">描述</h2>

<p>由于 <code>min</code><code>Math</code> 的静态方法,所以应该像这样使用:<code>Math.min()</code>,而不是作为你创建的 <code>Math</code> 实例的方法(Math 不是构造函数)。</p>

<p>如果没有参数,结果为{{jsxref("Infinity")}}</p>

<p>如果有任一参数不能被转换为数值,结果为 {{jsxref("NaN")}}</p>

<h2 id="Examples" name="Examples">示例</h2>

<h3 id="Example:_Using_Math.min" name="Example:_Using_Math.min">使用 <code>Math.min()</code></h3>

<p>下例找出 x 和 y 的最小值,并把它赋值给 z:</p>

<pre class="brush:js">var x = 10, y = -20;
var z = Math.min(x, y);
</pre>

<h3 id="Example.3A_Clipping_a_value_with_Math.min" name="Example.3A_Clipping_a_value_with_Math.min">使用 <code>Math.min()</code> 裁剪值(Clipping a value)</h3>

<p><code>Math.min</code> 经常用于裁剪一个值,以便使其总是小于或等于某个边界值。例如:</p>

<pre class="brush: js">var x = f(foo);

if (x &gt; boundary) {
    x = boundary;
}</pre>

<p>可以写成:</p>

<pre class="brush:js">var x = Math.min(f(foo), boundary);</pre>

<div>另外,{{jsxref("Math.max()")}} 也可以被用来以相似的方式裁剪一个值。</div>

<div> </div>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范版本</th>
   <th scope="col">规范状态</th>
   <th scope="col">注解</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition. Implemented in JavaScript 1.0</td>
   <td>{{Spec2('ES1')}}</td>
   <td>初始定义。在  JavaScript 1.0 中实现。</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.8.2.12', 'Math.min')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-math.min', 'Math.min')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.min', 'Math.min')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{ CompatibilityTable() }}</p>

<div>
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div>
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also" name="See_also">相关链接</h2>

<ul>
 <li>{{jsxref("Math.max()")}}</li>
</ul>