<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> <span style="color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px; white-space:pre-wrap">来自:http://www.liaoxuefeng.com/</span></p>
<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> <span style="color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px; white-space:pre-wrap">JavaScript不区分整数和浮点数,统一用Number表示</span><br> </p>
<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> JavaScript在设计时,有两种比较运算符:</p>
<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> 第一种是<code style="font-size:12px; font-family:Consolas,monospace,serif; color:rgb(221,0,85); white-space:nowrap; padding:0px 4px; border:1px solid rgb(221,221,221); background:rgb(250,250,250)">==</code>比较,它会自动转换数据类型再比较,很多时候,会得到非常诡异的结果;</p>
<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> 第二种是<code style="font-size:12px; font-family:Consolas,monospace,serif; color:rgb(221,0,85); white-space:nowrap; padding:0px 4px; border:1px solid rgb(221,221,221); background:rgb(250,250,250)">===</code>比较,它不会自动转换数据类型,如果数据类型不一致,返回<code style="font-size:12px; font-family:Consolas,monospace,serif; color:rgb(221,0,85); white-space:nowrap; padding:0px 4px; border:1px solid rgb(221,221,221); background:rgb(250,250,250)">false</code>,如果一致,再比较。</p>
<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> 由于JavaScript这个设计缺陷,<span style="color:rgb(221,0,85)">不要</span>使用<code style="font-size:12px; font-family:Consolas,monospace,serif; color:rgb(221,0,85); white-space:nowrap; padding:0px 4px; border:1px solid rgb(221,221,221); background:rgb(250,250,250)">==</code>比较,始终坚持使用<code style="font-size:12px; font-family:Consolas,monospace,serif; color:rgb(221,0,85); white-space:nowrap; padding:0px 4px; border:1px solid rgb(221,221,221); background:rgb(250,250,250)">===</code>比较。</p>
<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> </p>
<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> 另一个例外是<code style="font-size:12px; font-family:Consolas,monospace,serif; color:rgb(221,0,85); white-space:nowrap; padding:0px 4px; border:1px solid rgb(221,221,221); background:rgb(250,250,250)">NaN</code>这个特殊的Number与所有其他值都不相等,包括它自己:</p>
<pre style="margin-top:15px; margin-bottom:15px; padding:10px; line-height:18px; font-family:Consolas,monospace,serif; color:rgb(68,68,68); overflow:auto; border:1px solid rgb(221,221,221); word-break:break-all; word-wrap:break-word; white-space:pre-wrap; background:rgb(250,250,250)"><code class="javascript"><span class="literal">NaN</span> === <span class="literal">NaN</span>; <span class="comment" style="color:rgb(153,153,136); font-style:italic">// false</span>
</code></pre>
<p style="margin-top:15px; margin-bottom:15px; word-wrap:break-word; white-space:pre-wrap; color:rgb(102,102,102); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:14px; line-height:20px"> 唯一能判断<code style="font-size:12px; font-family:Consolas,monospace,serif; color:rgb(221,0,85); white-space:nowrap; padding:0px 4px; border:1px solid rgb(221,221,221); background:rgb(250,250,250)">NaN</code>的方法是通过<code style="font-size:12px; font-family:Consolas,monospace,serif; color:rgb(221,0,85); white-space:nowrap; padding:0px 4px; border:1px solid rgb(221,221,221); background:rgb(250,250,250)">isNaN()</code>函数:</p>
<pre style="margin-top:15px; margin-bottom:15px; padding:10px; line-height:18px; font-family:Consolas,monospace,serif; color:rgb(68,68,68); overflow:auto; border:1px solid rgb(221,221,221); word-break:break-all; word-wrap:break-word; white-space:pre-wrap; background:rgb(250,250,250)"><code class="ruby">isNaN(<span class="constant" style="color:rgb(0,153,153)">NaN</sp |
|