uTools-Manuals/docs/javascript/Reference/Errors/Var_hides_argument.html
2019-04-21 11:50:48 +08:00

32 lines
1.8 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">TypeError: variable "x" redeclares argument (Firefox)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> 警告仅仅在 <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式下</a> 出现。</p>
<h2 id="哪里有问题?">哪里有问题?</h2>
<p>函数参数中出现了名称相同的变量,之后在函数体中使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code> 赋值语句重新声明。 这可能是一个命名冲突,所以 JavaScript 警告了它。</p>
<p>这个错误只在 <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式的代码</a> 中作为警告出现。在非严格模式的代码中,重新声明会被忽略。</p>
<h2 id="示例">示例</h2>
<h3 id="无效情况">无效情况</h3>
<p>这个例子中,变量 <code>arg</code> 重新声明了参数。</p>
<pre><code class="language-js example-bad">"use strict";
function f(arg) {
var arg = "foo";
}
</code></pre>
<h3 id="无效情况_2">无效情况</h3>
<p>为了修复警告,<code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code> 语句应该被移除,因为变量已经存在。或者,你可以重命名函数参数或者变量名称。</p>
<pre><code class="language-js example-good">"use strict";
function f(arg) {
arg = "foo";
}
</code></pre>
<h2 id="另见">另见</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">严格模式</a></li>
</ul>
</article>