aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/number/isfinite/index.html
blob: f9caca9ba163c4210821b678eed71c9f15b6fbe6 (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
---
title: Number.isFinite()
slug: Web/JavaScript/Reference/Global_Objects/Number/isFinite
tags:
  - Experimental
  - JavaScript
  - Method
  - Number
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Number/isFinite
---
<div>{{JSRef}}</div>

<p><strong><code>Number.isFinite()</code></strong> 方法用来检测传入的参数是否是一个有穷数。</p>

<p>{{EmbedInteractiveExample("pages/js/number-isfinite.html")}}</p>

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

<pre class="syntaxbox"><code>Number.isFinite(v<var>alue</var>)</code></pre>

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

<dl>
 <dt><code>value</code></dt>
 <dd>要被检测有穷性的值。</dd>
</dl>

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

<p>一个 {{jsxref("Boolean", "布尔值")}} 表示给定的值是否是一个有穷数。</p>

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

<p>和全局的 {{jsxref("Global_Objects/isFinite", "isFinite()")}} 函数相比,这个方法不会强制将一个非数值的参数转换成数值,这就意味着,只有数值类型的值,且是有穷的(finite),才返回 <code>true</code></p>

<h2 id="Polyfill">Polyfill</h2>

<pre class="brush: js">if (Number.isFinite === undefined) Number.isFinite = function(value) {
    return typeof value === 'number' &amp;&amp; isFinite(value);
}</pre>

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

<pre class="brush: js">Number.isFinite(Infinity);  // false
Number.isFinite(NaN);       // false
Number.isFinite(-Infinity); // false

Number.isFinite(0);         // true
Number.isFinite(2e64);      // true

Number.isFinite('0');       // false, would've been true with
                            // global isFinite('0')
Number.isFinite(null);      // false, would've been true with
                            // global isFinite(null)
</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范名称</th>
   <th scope="col">规范状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-number.isfinite', 'Number.isInteger')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-number.isfinite', 'Number.isInteger')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

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

<div>{{Compat("javascript.builtins.Number.isFinite")}}</div>

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

<ul>
 <li>The {{jsxref("Number")}} object it belongs to.</li>
 <li>The global function {{jsxref("isFinite")}}.</li>
</ul>