From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../errors/redeclared_parameter/index.html | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html (limited to 'files/zh-tw/web/javascript/reference/errors/redeclared_parameter') diff --git a/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html b/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html new file mode 100644 index 0000000000..e9ba8cbbe0 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html @@ -0,0 +1,57 @@ +--- +title: 'SyntaxError: redeclaration of formal parameter "x"' +slug: Web/JavaScript/Reference/Errors/Redeclared_parameter +translation_of: Web/JavaScript/Reference/Errors/Redeclared_parameter +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
SyntaxError: redeclaration of formal parameter "x" (Firefox)
+SyntaxError: Identifier "x" has already been declared (Chrome)
+
+ +

錯誤類型

+ +

{{jsxref("SyntaxError")}}

+ +

哪裡錯了?

+ +

當相同的變數名作為函式的參數、接著又在函式體(function body)內用了 let 重複宣告並指派時出現。在 JavaScript 裡面,不允許在相同的函式、或是作用域區塊(block scope)內重複宣告相同的 let 變數。

+ +

實例

+ +

在這裡,「arg」變數的參數被重複宣告。

+ +
function f(arg) {
+  let arg = 'foo';
+}
+
+// SyntaxError: redeclaration of formal parameter "arg"
+
+ +

If you want to change the value of "arg" in the function body, you can do so, but you do not need to declare the same variable again. In other words: you can omit the let keyword. If you want to create a new variable, you need to rename it as conflicts with the function parameter already.

+ +
function f(arg) {
+  arg = 'foo';
+}
+
+function f(arg) {
+  let bar = 'foo';
+}
+
+ +

相容性註解

+ + + +

參見

+ + -- cgit v1.2.3-54-g00ecf