uTools-Manuals/docs/javascript/Reference/Errors/Redeclared_parameter.html

40 lines
2.4 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 class="syntaxbox">SyntaxError: redeclaration of formal parameter "x" (Firefox)
SyntaxError: Identifier "x" has already been declared (Chrome)
SyntaxError: Cannot declare a let variable twice: 'x' (WebKit)
</pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a></p>
<h2 id="哪里出错了">哪里出错了?</h2>
<p>某个变量名称已经作为函数参数出现了,但是又使用了 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code> 在函数体里重声明了。在JavaScript 中不允许使用let在相同的函数或块范围内重新声明相同的变量。</p>
<h2 id="示例">示例</h2>
<p>在本例中,参数 "arg" 又重新声明了:</p>
<pre class="brush: js example-bad">function f(arg) {
let arg = "foo";
}
// SyntaxError: redeclaration of formal parameter "arg"
</pre>
<p>如果要更改函数体中的“arg”的值可以像下面一样但不需要再次声明同一个变量。 换句话说:你可以省略 let 关键字。 如果要创建一个新变量,则需要将其重命名,因为其与函数参数有冲突。</p>
<pre class="brush: js example-good">function f(arg) {
arg = "foo";
}
function f(arg) {
let bar = "foo";
}
</pre>
<h2 id="兼容性提醒">兼容性提醒</h2>
<ul>
<li>在 Firefox 49 (Firefox 49 / Thunderbird 49 / SeaMonkey 2.46) 之前,会抛出的是 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> (<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1275240" rel="noopener" title="FIXED: Redeclaration of formal parameter with lexical binding should be SyntaxError">bug 1275240</a>)。</li>
</ul>
<h2 id="相关">相关</h2>
<ul>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code></li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code></li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li>
<li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_Types#Declarations">Declaring variables</a> in the <a href="/en-US/docs/Web/JavaScript/Guide">JavaScript Guide</a></li>
</ul>
</article>