ReferenceError
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Đối tượng ReferenceError biểu diễn một lỗi khi một biến không tồn tại (hoặc chưa được khởi tạo) trong phạm vi hiện tại được tham chiếu.
ReferenceError là một serializable object, vì vậy nó có thể được nhân bản với structuredClone() hoặc sao chép giữa các Worker bằng cách sử dụng postMessage().
ReferenceError là lớp con của Error.
Constructor
ReferenceError()-
Tạo một đối tượng
ReferenceErrormới.
Thuộc tính instance
Cũng kế thừa các thuộc tính instance từ lớp cha Error.
Các thuộc tính này được định nghĩa trên ReferenceError.prototype và được chia sẻ bởi tất cả các instance ReferenceError.
ReferenceError.prototype.constructor-
Hàm constructor đã tạo ra đối tượng instance. Đối với các instance
ReferenceError, giá trị khởi tạo là constructorReferenceError. ReferenceError.prototype.name-
Đại diện cho tên của loại lỗi. Đối với
ReferenceError.prototype.name, giá trị khởi tạo là"ReferenceError".
Phương thức instance
Kế thừa các phương thức instance từ lớp cha Error.
Ví dụ
>Bắt một ReferenceError
try {
let a = undefinedVariable;
} catch (e) {
console.log(e instanceof ReferenceError); // true
console.log(e.message); // "undefinedVariable is not defined"
console.log(e.name); // "ReferenceError"
console.log(e.stack); // Stack of the error
}
Tạo một ReferenceError
try {
throw new ReferenceError("Hello");
} catch (e) {
console.log(e instanceof ReferenceError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "ReferenceError"
console.log(e.stack); // Stack of the error
}
Đặc tả kỹ thuật
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-native-error-types-used-in-this-standard-referenceerror> |