diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators/equality')
-rw-r--r-- | files/zh-cn/web/javascript/reference/operators/equality/index.html | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/equality/index.html b/files/zh-cn/web/javascript/reference/operators/equality/index.html new file mode 100644 index 0000000000..fadd413b17 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/equality/index.html @@ -0,0 +1,126 @@ +--- +title: 相等(==) +slug: Web/JavaScript/Reference/Operators/Equality +tags: + - JavaScript + - Reference +translation_of: Web/JavaScript/Reference/Operators/Equality +original_slug: Web/JavaScript/Reference/Operators/相等 +--- +<div>{{jsSidebar("Operators")}}</div> + +<p>等于运算符(<code>==</code>)检查其两个操作数是否相等,并返回<code>Boolean</code>结果。与<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality">严格相等</a>运算符(<code>===</code>)不同,它会尝试强制类型转换并且比较不同类型的操作数。</p> + +<div>{{EmbedInteractiveExample("pages/js/expressions-equality.html")}}</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">x == y +</pre> + +<h2 id="描述">描述</h2> + +<p>相等运算符(<code>==</code>和<code>!=</code>)使用<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3">抽象相等比较算法</a>比较两个操作数。可以大致概括如下:</p> + +<ul> + <li>如果两个操作数都是对象,则仅当两个操作数都引用同一个对象时才返回<code>true</code>。</li> + <li>如果一个操作数是<code>null</code>,另一个操作数是<code>undefined</code>,则返回<code>true</code>。</li> + <li>如果两个操作数是不同类型的,就会尝试在比较之前将它们转换为相同类型: + <ul> + <li>当数字与字符串进行比较时,会尝试将字符串转换为数字值。</li> + <li>如果操作数之一是<code>Boolean</code>,则将布尔操作数转换为1或0。 + <ul> + <li>如果是<code>true</code>,则转换为<code>1</code>。</li> + <li>如果是 <code>false</code>,则转换为<code>0</code>。</li> + </ul> + </li> + <li>如果操作数之一是对象,另一个是数字或字符串,会尝试使用对象的<code>valueOf()</code>和<code>toString()</code>方法将对象转换为原始值。</li> + </ul> + </li> + <li>如果操作数具有相同的类型,则将它们进行如下比较: + <ul> + <li><code>String</code>:<code>true</code>仅当两个操作数具有相同顺序的相同字符时才返回。</li> + <li><code>Number</code>:<code>true</code>仅当两个操作数具有相同的值时才返回。<code>+0</code>并被<code>-0</code>视为相同的值。如果任一操作数为<code>NaN</code>,则返回<code>false</code>。</li> + <li><code>Boolean</code>:<code>true</code>仅当操作数为两个<code>true</code>或两个<code>false</code>时才返回<code>true</code>。</li> + </ul> + </li> +</ul> + +<p>此运算符与<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality">严格等于</a>(<code>===</code>)运算符之间最显着的区别在于,严格等于运算符不尝试类型转换。相反,严格相等运算符始终将不同类型的操作数视为不同。</p> + +<h2 id="例子">例子</h2> + +<h3 id="没有类型转换的比较">没有类型转换的比较</h3> + +<pre class="brush: js notranslate">1 == 1; // true +"hello" == "hello"; // true</pre> + +<h3 id="与类型转换比较">与类型转换比较</h3> + +<pre class="brush: js notranslate">"1" == 1; // true +1 == "1"; // true +0 == false; // true +0 == null; // false +0 == undefined; // false +null == undefined; // true + +const number1 = new Number(3); +const number2 = new Number(3); +number1 == 3; // true +number1 == number2; // false</pre> + +<h3 id="对象比较">对象比较</h3> + +<pre class="brush: js notranslate">const object1 = {"key": "value"} +const object2 = {"key": "value"}; + +object1 == object2 // false +object2 == object2 // true</pre> + +<h3 id="比较字符串和String对象">比较字符串和String对象</h3> + +<p>请注意,使用构造的字符串<code>new String()</code>是对象。如果将其中之一与字符串文字进行比较,则该<code>String</code>对象将被转换为字符串文字并对其内容进行比较。但是,如果两个操作数都是<code>String</code>对象,则将它们作为对象进行比较,并且必须引用相同的对象才能进行比较:</p> + +<pre class="brush: js notranslate">const string1 = "hello"; +const string2 = String("hello"); +const string3 = new String("hello"); +const string4 = new String("hello"); + +console.log(string1 == string2); // true +console.log(string1 == string3); // true +console.log(string2 == string3); // true +console.log(string3 == string4); // false +console.log(string4 == string4); // true</pre> + +<h3 id="比较日期和字符串">比较日期和字符串</h3> + +<pre class="brush: js notranslate">const d = new Date('December 17, 1995 03:24:00'); +const s = d.toString(); // for example: "Sun Dec 17 1995 03:24:00 GMT-0800 (Pacific Standard Time)" +console.log(d == s); //true</pre> + +<h2 id="技术指标">技术指标</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-equality-operators', 'Equality operators')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("javascript.operators.equality")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Inequality">不等式运算符</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality">严格相等运算符</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality">严格的不等式运算符</a></li> +</ul> |