SyntaxError: duplicate formal argument x
Ngoại lệ JavaScript "duplicate formal argument x" hoặc "duplicate argument names not allowed in this context" xảy ra khi một hàm tạo ra hai hay nhiều ràng buộc tham số có cùng tên, và hàm đó không phải là hàm không ở chế độ strict với chỉ các tham số đơn giản.
Thông báo
SyntaxError: Duplicate parameter name not allowed in this context (V8-based) SyntaxError: duplicate formal argument x (Firefox) SyntaxError: duplicate argument names not allowed in this context (Firefox) SyntaxError: Cannot declare a parameter named 'x' in strict mode as it has already been declared. (Safari) SyntaxError: Duplicate parameter 'x' not allowed in function with default parameter values. (Safari) SyntaxError: Duplicate parameter 'x' not allowed in function with a rest parameter. (Safari) SyntaxError: Duplicate parameter 'x' not allowed in function with destructuring parameters. (Safari)
Loại lỗi
SyntaxError
Nguyên nhân?
Việc có hai tham số chính thức cùng tên rất có thể là một lỗi — lần xuất hiện thứ hai sẽ khiến lần xuất hiện đầu tiên không thể truy cập được qua tên tham số. Trong JavaScript cũ, điều này được cho phép. Do đó, để không phá vỡ code hiện có, đây chỉ là lỗi nếu code được đảm bảo không phải là code cũ — hoặc vì nó ở trong chế độ strict hoặc nó sử dụng cú pháp tham số hiện đại (tham số rest, mặc định, hoặc destructured).
Ví dụ
>Các trường hợp không hợp lệ
js
"use strict";
function add(x, x) {
// How can you access both "x" parameters?
// SyntaxError: duplicate formal argument x
}
js
function doSomething(name, { name }) {
// How can you access both "name" parameters?
// SyntaxError: duplicate argument names not allowed in this context
}
Các trường hợp hợp lệ
js
function doSomething(operationName, { name: userName }) {
// You can access both "operationName" and "userName" parameters.
}
function doSomething(name, user) {
// You can access both "name" and "user.name" parameters.
}