mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 15:34:05 +08:00
211 lines
14 KiB
HTML
211 lines
14 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<p>JavaScript 有两种比较方式:严格比较运算符和转换类型比较运算符。对于严格比较运算符(===)来说,仅当两个操作数的类型相同且值相等为 true,而对于被广泛使用的比较运算符(==)来说,会在进行比较之前,将两个操作数转换成相同的类型。对于关系运算符(比如 <=)来说,会先将操作数转为原始值,使它们类型相同,再进行比较运算。</p>
|
||
<p>字符串比较则是使用基于标准字典的 Unicode 值来进行比较的。</p>
|
||
<p>比较的特点:</p>
|
||
<ul>
|
||
<li>对于两个拥有相同字符顺序,相同长度,并且每个字符的位置都匹配的字符串,应该使用严格比较运算符。</li>
|
||
<li><span style="line-height: 1.5;"> 对于两个数值相同的数字应该使用严格比较运算符,NaN和任何值不相等,包括其自身,正数零等于负数零。</span></li>
|
||
<li>对于两个同为true或同为false的布尔操作数,应使用严格比较运算符。</li>
|
||
<li>不要使用严格比较运算符或比较运算符来比较两个不相等的对象。</li>
|
||
<li>当比较一个表达式和一个对象时,仅当两个操作数引用相同的对象(指针指向相同对象)。</li>
|
||
<li>对于Null 和 Undefined 类型而言,应使用严格比较运算符比较其自身,使用比较运算符进行互相比较。</li>
|
||
</ul>
|
||
<h2 id="相等运算符">相等运算符</h2>
|
||
<h3 id="相等()">相等(==)</h3>
|
||
<p>比较操作符会为两个不同类型的操作数转换类型,然后进行严格比较。当两个操作数都是对象时,JavaScript会比较其内部引用,当且仅当他们的引用指向内存中的相同对象(区域)时才相等,即他们在栈内存中的引用地址相同。</p>
|
||
<h4 id="语法">语法</h4>
|
||
<pre><code class="language-javascript">x == y
|
||
</code></pre>
|
||
<h4 id="例子">例子</h4>
|
||
<pre><code class="language-javascript"> 1 == 1 // true
|
||
"1" == 1 // true
|
||
1 == '1' // true
|
||
0 == false // true
|
||
</code></pre>
|
||
<h3 id="不相等_(!)"><a name="Inequality">不相等 (!=)</a></h3>
|
||
<p>不等操作符仅当操作数不相等时返回true,如果两操作数不是同一类型,JavaScript会尝试将其转为一个合适的类型,然后进行比较。如果两操作数为对象类型,JavaScript会比较其内部引用地址,仅当他们在内存中引用不同对象时不相等。</p>
|
||
<h4 id="语法_2">语法</h4>
|
||
<pre><code class="language-javascript">x != y</code></pre>
|
||
<h4 id="例子_2">例子</h4>
|
||
<pre><code class="language-javascript">1 != 2 // true
|
||
1 != "1" // false
|
||
1 != '1' // false
|
||
1 != true // false
|
||
0 != false // false
|
||
</code></pre>
|
||
<h3 id="一致严格相等_()"><a name="Identity">一致/严格相等 (===)</a></h3>
|
||
<p>一致运算符不会进行类型转换,仅当操作数严格相等时返回true</p>
|
||
<h4 id="语法_3">语法</h4>
|
||
<pre><code class="language-javascript">x === y</code></pre>
|
||
<h4 id="例子_3">例子</h4>
|
||
<pre><code class="language-js ">3 === 3 // true
|
||
3 === '3' // false
|
||
var object1 = {"value":"key"}, object2={"value":"key"};
|
||
object1 === object2 //false</code></pre>
|
||
<h3 id="不一致严格不相等_(!)"><a name="Nonidentity">不一致/严格不相等 (!==)</a></h3>
|
||
<p>不一致运算符当操作数不相等或不同类型时返回true</p>
|
||
<h4 id="语法_4">语法</h4>
|
||
<pre><code class="language-javascript">x !== y</code></pre>
|
||
<h4 id="例子_4">例子</h4>
|
||
<pre><code class="language-javascript">3 !== '3' // true
|
||
4 !== 3 // true
|
||
</code></pre>
|
||
<h2 id="关系运算符">关系运算符</h2>
|
||
<h3 id="大于运算符_(>)"><a name="Greater_than_operator">大于运算符 (>)</a></h3>
|
||
<p>大于运算符仅当左操作数大于右操作数时返回true</p>
|
||
<h4 id="语法_5">语法</h4>
|
||
<pre><code class="language-javascript">x > y</code></pre>
|
||
<h4 id="例子_5">例子</h4>
|
||
<pre><code class="language-javascript">4 > 3 // true
|
||
</code></pre>
|
||
<h3 id="大于等于运算符_(>)"><a name="Greater_than_or_equal_operator">大于等于运算符 (>=)</a></h3>
|
||
<p>大于等于运算符当左操作数大于或等于右操作数时返回true</p>
|
||
<h4 id="语法_6">语法</h4>
|
||
<pre><code class="language-javascript"> x >= y</code></pre>
|
||
<h4 id="例子_6">例子</h4>
|
||
<pre><code class="language-javascript">4 >= 3 // true
|
||
3 >= 3 // true
|
||
</code></pre>
|
||
<h3 id="小于运算符_(<)"><a name="Less_than_operator">小于运算符 (<)</a></h3>
|
||
<p>小于运算符仅当左操作数小于右操作数时返回true</p>
|
||
<h4 id="语法_7">语法</h4>
|
||
<pre><code class="language-javascript"> x < y</code></pre>
|
||
<h4 id="例子_7">例子</h4>
|
||
<pre><code class="language-javascript">3 < 4 // true
|
||
</code></pre>
|
||
<h3 id="小于等于运算符_(<)"><a id="Less_than_or_equal_operator" name="Less_than_or_equal_operator">小于等于运算符 (<=)</a></h3>
|
||
<p>小于等于运算符当左操作数小于或等于右操作数时返回true</p>
|
||
<h4 id="语法_8">语法</h4>
|
||
<pre><code class="language-javascript"> x <= y</code></pre>
|
||
<h4 id="例子_8">例子</h4>
|
||
<pre><code class="language-javascript">3 <= 4 // true
|
||
</code></pre>
|
||
<h2 id="使用比较操作符">使用比较操作符</h2>
|
||
<p>标准相等操作符(<code>==</code> and <code>!=</code>) 使用 <a class="external" href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3" rel="noopener">Abstract Equality Comparison Algorithm</a> 去比较两个操作数。当两个操作数类型不相等时,会在比较前尝试将其转换为相同类型。 e.g., 对于表达式 <code>5 == '5'</code>, 在比较前会先将右边字符串类型的操作数 5 转换为数字。</p>
|
||
<p>严格相等操作符 (<code>===</code> and <code>!==</code>) 使用 Strict Equality Comparison Algorithm 并尝试对两个相同操作数进行相等比较,如果它们的类型不相等,那么永远会返回false 所以 <code>5 !== '5'。</code></p>
|
||
<p>当需要明确操作数的类型和值的时候,或者操作数的确切类型非常重要时,应使用严格相等操作符。否则,当你允许操作数在比较前进行类型转换时,可以使用标准相等操作符来比较。</p>
|
||
<p>当比较运算涉及类型转换时 (i.e., non–strict comparison), JavaScript 会按以下规则对字符串,数字,布尔或对象类型的操作数进行操作:</p>
|
||
<ul>
|
||
<li>当比较数字和字符串时,字符串会转换成数字值。 JavaScript 尝试将数字字面量转换为数字类型的值。 首先, 一个数学上的值会从数字字面量中衍生出来,然后得到被四舍五入后的数字类型的值。</li>
|
||
<li>如果其中一个操作数为布尔类型,那么布尔操作数如果为true,那么会转换为1,如果为false,会转换为整数0,即0。</li>
|
||
<li>如果一个对象与数字或字符串相比较,JavaScript会尝试返回对象的默认值。操作符会尝试通过方法valueOf和toString将对象转换为其原始值(一个字符串或数字类型的值)。如果尝试转换失败,会产生一个运行时错误。</li>
|
||
<li>注意:当且仅当与原始值比较时,对象会被转换为原始值。当两个操作数均为对象时,它们作为对象进行比较,仅当它们引用相同对象时返回true。</li>
|
||
</ul>
|
||
<div class="note"><strong>注意:</strong> 字符串对象的类型是对象,不是字符串!字符串对象很少被使用,所以下面的结果也许会让你惊讶:</div>
|
||
<pre><code class="language-js">// true as both operands are Type String (i.e. string primitives):
|
||
'foo' === 'foo'
|
||
|
||
var a = new String('foo');
|
||
var b = new String('foo');
|
||
|
||
// false as a and b are Type Object and reference different objects
|
||
a == b
|
||
|
||
// false as a and b are Type Object and reference different objects
|
||
a === b
|
||
|
||
// true as a and 'foo' are of different type and, the Object (a)
|
||
// is converted to String 'foo' before comparison
|
||
a == 'foo' </code></pre>
|
||
<h2 id="Specifications">Specifications</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>ECMAScript 1st Edition.</td>
|
||
<td>Standard</td>
|
||
<td>Initial definition. Implemented in JavaScript 1.0</td>
|
||
</tr>
|
||
<tr>
|
||
<td>ECMAScript 3rd Edition.</td>
|
||
<td>Standard</td>
|
||
<td>Adds <code>===</code> and <code>!==</code> operators. Implemented in JavaScript 1.3</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-11.8" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">Relational Operators</small></a><br/>
|
||
<a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-11.9" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">Equality Operators</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>Defined in several sections of the specification: <a class="external" href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.8" rel="noopener">Relational Operators</a>, <a class="external" href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9" rel="noopener">Equality Operators</a></td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-relational-operators" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Relational Operators</small></a><br/>
|
||
<a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-equality-operators" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Equality Operators</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>Defined in several sections of the specification: <a class="external" href="http://www.ecma-international.org/ecma-262/6.0/#sec-relational-operators" rel="noopener">Relational Operators</a>, <a class="external" href="http://www.ecma-international.org/ecma-262/6.0/#sec-equality-operators" rel="noopener">Equality Operators</a></td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-relational-operators" hreflang="en" lang="en" rel="noopener" title="ECMAScript Latest Draft (ECMA-262)">ECMAScript Latest Draft (ECMA-262)</a></td>
|
||
<td><span class="spec-Draft">Draft</span></td>
|
||
<td>Defined in several sections of the specification: <a class="external" href="http://tc39.github.io/ecma262/#sec-relational-operators" rel="noopener">Relational Operators</a>, <a class="external" href="http://tc39.github.io/ecma262/#sec-equality-operators" rel="noopener">Equality Operators</a></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="Browser_compatibility">Browser compatibility</h2>
|
||
<p></p><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><p></p>
|
||
<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><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</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: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h2 id="See_also">See also</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Object/is" title="Object.is()方法判断两个值是否是相同的值。"><code>Object.is()</code></a></li>
|
||
<li><a href="/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness">Equality comparisons and sameness</a></li>
|
||
</ul>
|
||
</article> |