SyntaxError: use of super property/member accesses only valid within methods or eval code within methods
Ngoại lệ JavaScript "use of super property/member accesses only valid within methods or eval code within methods" xảy ra khi cú pháp super.x hoặc super[x] được sử dụng bên ngoài một phương thức.
Thông báo
SyntaxError: 'super' keyword unexpected here (V8-based) SyntaxError: use of super property accesses only valid within methods or eval code within methods (Firefox) SyntaxError: super is not valid in this context. (Safari)
Loại lỗi
SyntaxError
Nguyên nhân?
Cú pháp super.x được dùng để truy cập các thuộc tính trên prototype của đối tượng hiện tại. Nó có thể được sử dụng trong các phương thức của cả object literals và classes, field initializers, và static initialization blocks, nhưng không trong các ngữ cảnh khác.
Ví dụ
>Trường hợp không hợp lệ
Bạn không thể sử dụng super.x bên ngoài một phương thức trong một đối tượng:
const obj = {
__proto__: { x: 1 },
x: super.x, // SyntaxError: use of super property accesses only valid within methods or eval code within methods
};
Bạn không thể sử dụng super.x trong một hàm, ngay cả khi hàm đó có tác dụng như một phương thức:
function getX() {
return super.x; // SyntaxError: use of super property accesses only valid within methods or eval code within methods
}
const obj = {
getX,
getX2: function () {
return super.x; // SyntaxError: use of super property accesses only valid within methods or eval code within methods
},
};
class Derived extends Base {
getX = () => super.x;
}
Trường hợp hợp lệ
Bạn có thể sử dụng super.x trong một phương thức:
class Base {
x = 1;
}
class Derived extends Base {
getX() {
return super.x;
}
}
Bạn có thể sử dụng super.x trong một field initializer:
class Derived extends Base {
x = super.x;
}
Bạn cũng có thể sử dụng super.x trong các phương thức của đối tượng:
const obj = {
__proto__: { x: 1 },
getX() {
return super.x;
},
};