mozilla

Revision 451083 of Object.is()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/is
  • Revision title: is
  • Revision id: 451083
  • Created:
  • Creator: zhangyaochun1987
  • Is current revision? No
  • Comment 补充丢失的东西

Revision Content

{{harmony}}
{{fx_minversion_header("22")}}

概述

判断两个值是否真正是同一个值.

Method of Object
Implemented in JavaScript 1.8.5+
ECMAScript Edition ECMAScript 6th Edition

语法

var isSame = Object.is(value1,value2)

参数

value1
需要比较的第一个值
value1
需要比较的第二个值

描述

类似于严格相等===运算符,除了两个特例情况:就是NaN和NaN比较,以及+0和-0比较.

示例

Object.is("foo","foo");     // true
Object.is(window,window);   // true

Object.is("foo","bar");     // false
Object.is([],[]);           // false

//两个特例
Object.is(0,-0);            // false
Object.is(NaN,0/0);         // true

兼容性

if (!Object.is) { Object.is = function(v1, v2) { if (v1 === 0 && v2 === 0) return 1 / v1 === 1 / v2; if (v1 !== v1) return v2 !== v2; return v1 === v2; }; }

 

浏览器兼容性

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}}

{{CompatGeckoDesktop("22")}}

{{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoDesktop("22")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

 

Revision Source

<div>
  {{harmony}}</div>
<div>
  {{fx_minversion_header("22")}}</div>
<h2 id=".E6.A6.82.E8.BF.B0">概述</h2>
<p>判断两个值是否真正是同一个值.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of<code> <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object">Object</a></code></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.5+</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 6th Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">
<code>var isSame = Object.is(value1,value2)</code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
  <dt>
    <em><code>value1</code></em></dt>
  <dd>
    需要比较的第一个值</dd>
  <dt>
    <em><code>value1</code></em></dt>
  <dd>
    需要比较的第二个值</dd>
</dl>
<h2 id=".E6.8F.8F.E8.BF.B0">描述</h2>
<p><span class="short_text" id="result_box" lang="en"><span>类似于</span><span class="hps">严格相等===运算符,除了两个特例</span></span>情况:就是NaN和NaN比较,以及+0和-0比较.</p>
<h2 id=".E7.A4.BA.E4.BE.8B">示例</h2>
<pre class="brush:js;">
Object.is("foo","foo");     // true
Object.is(window,window);   // true

Object.is("foo","bar");     // false
Object.is([],[]);           // false

//两个特例
Object.is(0,-0);            // false
Object.is(NaN,0/0);         // true</pre>
<h2 id="Compatibility" name="Compatibility" style="margin: 0px 0px 0.8em; padding: 0px; font-size: 1.428em; font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'DejaVu Sans', Lucida, Arial, Helvetica, sans-serif;">兼容性</h2>
<h2 id=".E6.B5.8F.E8.A7.88.E5.99.A8.E5.85.BC.E5.AE.B9.E6.80.A7"><span style="font-family: 'Courier New', 'Andale Mono', monospace; font-size: 14px; font-weight: normal;">if (!Object.is) { Object.is = function(v1, v2) { if (v1 === 0 &amp;&amp; v2 === 0) return 1 / v1 === 1 / v2; if (v1 !== v1) return v2 !== v2; return v1 === v2; }; }</span></h2>
<p>&nbsp;</p>
<h2>浏览器兼容性</h2>
<div>
  {{CompatibilityTable}}</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>{{CompatNo}}</td>
        <td>
          <p>{{CompatGeckoDesktop("22")}}</p>
        </td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>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>{{CompatNo}}</td>
        <td>{{CompatGeckoDesktop("22")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision