124 lines
7.7 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<p><strong><code>Number.isNaN()</code></strong> 方法确定传递的值是否为 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字Not-A-Number。"><code>NaN</code></a>和其类型是 <a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a>。它是原始的全局<a href="Reference/Global_Objects/isNaN" title="isNaN() 函数用来确定一个值是否为NaN 。注isNaN函数内包含一些非常有趣的规则你也可以通过ECMAScript 2015/ES6 中定义的Number.isNaN()或者 可以使用typeof 来判断该值是否为一个非数字。"><code>isNaN()</code></a>的更强大的版本。</p>
<p><iframe class="interactive interactive-js taller" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/number-isnan.html" width="100%"></iframe></p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code>Number.isNaN(v<var>alue</var>)</code></code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>value</code></dt>
<dd>要被检测是否是 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字Not-A-Number。"><code>NaN</code></a> 的值。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>一个<a href="Reference/Boolean" title="此页面仍未被本地化, 期待您的翻译!"><code>布尔值</code></a>,表示给定的值是否是 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字Not-A-Number。"><code>NaN</code></a></p>
<h2 id="描述">描述</h2>
<p>在 JavaScript 中,<code>NaN</code> 最特殊的地方就是,我们不能使用相等运算符(<a href="Reference/Operators/Comparison_Operators#Equality" title="JavaScript 有两种比较方式:严格比较运算符和转换类型比较运算符。对于严格比较运算符(===)来说,仅当两个操作数的类型相同且值相等为 true而对于被广泛使用的比较运算符==)来说,会在进行比较之前,将两个操作数转换成相同的类型。对于关系运算符(比如 &lt;=)来说,会先将操作数转为原始值,使它们类型相同,再进行比较运算。"><code>==</code></a> 和 <a href="Reference/Operators/Comparison_Operators#Identity" title="JavaScript 有两种比较方式:严格比较运算符和转换类型比较运算符。对于严格比较运算符(===)来说,仅当两个操作数的类型相同且值相等为 true而对于被广泛使用的比较运算符==)来说,会在进行比较之前,将两个操作数转换成相同的类型。对于关系运算符(比如 &lt;=)来说,会先将操作数转为原始值,使它们类型相同,再进行比较运算。"><code>===</code></a>)来判断一个值是否是 <code>NaN</code>,因为 <code>NaN == NaN</code><code>NaN === NaN</code> 都会返回 <code>false</code>。因此,必须要有一个判断值是否是 <code>NaN</code> 的方法。</p>
<p>和全局函数 <a href="Reference/Global_Objects/isNaN" title="isNaN() 函数用来确定一个值是否为NaN 。注isNaN函数内包含一些非常有趣的规则你也可以通过ECMAScript 2015/ES6 中定义的Number.isNaN()或者 可以使用typeof 来判断该值是否为一个非数字。"><code>isNaN()</code></a> 相比,该方法不会强制将参数转换成数字,只有在参数是真正的数字类型,且值为 <code>NaN</code> 的时候才会返回 <code>true</code></p>
<h2 id="示例">示例</h2>
<pre><code class="language-javascript">Number.isNaN(NaN); // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0) // true
// 下面这几个如果使用全局的 isNaN() 时,会返回 true。
Number.isNaN("NaN"); // false字符串 "NaN" 不会被隐式转换成数字 NaN。
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN("blabla"); // false
// 下面的都返回 false
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);
Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");</code></pre>
<h2 id="Polyfill">Polyfill</h2>
<pre><code class="language-javascript">Number.isNaN = Number.isNaN || function(value) {
return typeof value === "number" &amp;&amp; isNaN(value);
}
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-number.isnan" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Number.isnan</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-number.isnan" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Number.isnan</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<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>25</td>
<td><a href="/en-US/Firefox/Releases/15" title="Released on 2012-08-28.">15</a> (15)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<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><span style="color: #f00;">未实现</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>15.0 (15)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a></li>
<li><a href="Reference/Global_Objects/isNaN" title="isNaN() 函数用来确定一个值是否为NaN 。注isNaN函数内包含一些非常有趣的规则你也可以通过ECMAScript 2015/ES6 中定义的Number.isNaN()或者 可以使用typeof 来判断该值是否为一个非数字。"><code>isNaN()</code></a></li>
</ul>
</article>