SyntaxError: arguments is not valid in fields
Ngoại lệ JavaScript "SyntaxError: arguments is not valid in fields" xảy ra khi định danh arguments được đọc trong bộ khởi tạo trường class hoặc trong khối khởi tạo tĩnh, bên ngoài một hàm không phải arrow function.
Thông báo
SyntaxError: 'arguments' is not allowed in class field initializer or static initialization block (V8-based) SyntaxError: arguments is not valid in fields (Firefox) SyntaxError: Unexpected identifier 'arguments'. Cannot reference 'arguments' in class field initializer. (Safari)
Loại lỗi
SyntaxError
Nguyên nhân?
Biểu thức bộ khởi tạo trường class hoặc khối khởi tạo tĩnh của class không có arguments trong phạm vi của nó. Việc cố gắng truy cập nó là một lỗi cú pháp.
- Điều này đúng ngay cả khi
argumentsđược liên kết trong phạm vi cha (chẳng hạn khi class được lồng bên trong một hàm không phải arrow function). - Hàm không phải arrow function được khai báo trong phạm vi này vẫn sẽ liên kết
argumentscủa chính nó và đọc nó bình thường.
Ví dụ
js
function makeOne() {
class C {
args = { ...arguments }; // SyntaxError: arguments is not valid in fields
}
return new C();
}
js
let CArgs;
class C {
static {
CArgs = arguments; // SyntaxError: arguments is not valid in fields
}
}
js
class C {
args = {};
constructor() {
this.args = arguments; // You can use arguments in constructors
}
myMethod() {
this.args = arguments; // You can also use it in methods
}
}
js
function makeOne() {
const _arguments = arguments;
class C {
args = { ..._arguments }; // Only the identifier is forbidden
}
return new C();
}