2019-04-21 11:50:48 +08:00

31 lines
2.0 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>
<h2 id="消息">消息</h2>
<pre><code class="language-javascript">Warning: SyntaxError: test for equality (==) mistyped as assignment (=)?
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a> 只在<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a>下会出现的警告。</p>
<h2 id="什么地方出错了">什么地方出错了?</h2>
<p>在通常期望进行相等判定(<code>==</code>)的地方出现了赋值<code>=</code>)。 为了帮助调试JavaScript在开启严格模式的情况下会对这种情况进行警告。</p>
<h2 id="示例">示例</h2>
<h3 id="条件表达式内的赋值">条件表达式内的赋值</h3>
<p>不建议在条件表达式中 (例如 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if...else</a></code> 使用简单赋值语句,因为在扫视代码的时候赋值操作与相等判定容易产生混淆。例如,不要使用以下写法:</p>
<pre><code class="language-js example-bad">if (x = y) {
// do the right thing
}
</code></pre>
<p>如果你需要在条件表达式中使用赋值语句, 通常的做法是用一对括号把赋值语句包起来。 例如:</p>
<pre><code class="language-javascript">if ((x = y)) {
// do the right thing
}</code></pre>
<p>否则, 你的本意可能是想用比较操作符 (如 <code>==</code><code>===</code></p>
<pre><code class="language-javascript">if (x == y) {
// do the right thing
}</code></pre>
<h2 id="相关页面">相关页面</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if...else</a></code></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">Comparison operators</a></li>
</ul>
</article>