diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/errors/redeclared_parameter | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/errors/redeclared_parameter')
-rw-r--r-- | files/zh-cn/web/javascript/reference/errors/redeclared_parameter/index.html | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/errors/redeclared_parameter/index.html b/files/zh-cn/web/javascript/reference/errors/redeclared_parameter/index.html new file mode 100644 index 0000000000..9a53795180 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/errors/redeclared_parameter/index.html @@ -0,0 +1,62 @@ +--- +title: 'SyntaxError: redeclaration of formal parameter "x"' +slug: Web/JavaScript/Reference/Errors/Redeclared_parameter +tags: + - Errors + - JavaScript + - SyntaxError +translation_of: Web/JavaScript/Reference/Errors/Redeclared_parameter +--- +<div>{{jsSidebar("Errors")}}</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>{{jsxref("SyntaxError")}}</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 {{geckoRelease(49)}} 之前,会抛出的是 {{jsxref("TypeError")}} ({{bug(1275240)}})。</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> |