The A Creates a new
Error message. Inherited from Error name. Inherited from TypeError
object represents an error when an operation could not be performed, typically [but not exclusively] when a value is not of the expected type.TypeError
may be thrown when:TypeError
is a serializable object, so it can be cloned with structuredClone[]
or copied between Workers using
postMessage[]
.Constructor
TypeError[]
TypeError
object.Instance properties
TypeError.prototype.message
Error
.TypeError.prototype.name
Error
.
TypeError.prototype.cause
Error cause. Inherited from Error
.
TypeError.prototype.fileName
Non-standardPath to file that raised this error.
Inherited from Error
.
TypeError.prototype.lineNumber
Non-standardLine number in file that raised this error. Inherited from
Error
.
TypeError.prototype.columnNumber
Non-standardColumn number in line that raised this error. Inherited from
Error
.
TypeError.prototype.stack
Non-standardStack trace. Inherited from
Error
.
Examples
Catching a TypeError
try {
null.f[]
} catch [e] {
console.log[e instanceof TypeError] // true
console.log[e.message] // "null has no properties"
console.log[e.name] // "TypeError"
console.log[e.fileName] // "Scratchpad/1"
console.log[e.lineNumber] // 2
console.log[e.columnNumber] // 2
console.log[e.stack] // "@Scratchpad/2:2:3\n"
}
Creating a TypeError
try {
throw new TypeError['Hello', "someFile.js", 10]
} catch [e] {
console.log[e instanceof TypeError] // true
console.log[e.message] // "Hello"
console.log[e.name] // "TypeError"
console.log[e.fileName] // "someFile.js"
console.log[e.lineNumber] // 10
console.log[e.columnNumber] // 0
console.log[e.stack] // "@Scratchpad/2:2:9\n"
}
Specifications
ECMAScript Language Specification # sec-native-error-types-used-in-this-standard-typeerror |
Browser compatibility
BCD tables only load in the browser
See also
Lượt xem: 3247 Ngày tạo: 04/02/2021 Ngày cập nhật: 17/02/2021
Uncaught TypeError: Assignment to constant variable là một lỗi rất cơ bản trong Javascript, thường gặp ở những coder mới tiếp xúc với Javascript.
Hiện tượng
- Kịch bản [code] Javascript không hoạt động hoặc bị ngắt quãng giữa chừng
- Javascript Console báo lỗi: Uncaught TypeError: Assignment to constant variable.
Nguyên nhân
Bạn đang cố ý [hoặc vô ý] làm thay đổi dữ liệu của Hằng [Constant] trong kịch bản Javascript. Hãy luôn nhớ rằng, bạn HOÀN TOÀN KHÔNG THỂ thay đổi dữ liệu [reassign] của Hằng kể từ khi bạn gán giá trị đầu tiên [assign] cho nó. Xem lại bài Biến [Variable] và Hằng [Constant] trong Javascript.
Cách khắc phục [fix]
Bước 1. Xác định vị trí Statement làm thay đổi giá trị của Hằng [Constant]
Trên giao diện Javascript Console của trình duyệt web, bạn click vào link báo lỗi Uncaught TypeError: Assignment to constant variable [nằm ở bên phải dòng lỗi].
Bước 2. Xử lý tuỳ theo tình huống
Tình huống 1: Kịch bản Javascript mà bạn đang muốn viết buộc bạn phải thay đổi giá trị của HằngHướng xử lý: Dùng Biến [variable] thay vì Hằng. Biến là thành phần phù hợp để có thể chứa dữ liệu và dữ liệu đó có thể thay đổi liên tục trong suốt kịch bản Javascript. Xem lại bài Biến [Variable] và Hằng [Constant] trong Javascript.
Ví dụ:
Trước khi fix | Sau khi fix |
| ... var KEY = '123'; // Dùng biến KEY = document.getElementById["key-input"].value;
|
Hướng xử lý: Xoá hoặc viết lại Statement đó để dữ liệu của Hằng không bị thay đổi
Và chúng ta cũng đã đi qua mục cuối cùng trong bài hướng dẫn rồi. Nếu bạn có bất kỳ thắc mắc nào, hay không thực hiện được bước nào, hãy để lại bình luận bên dưới nhé. Nếu bạn cảm thấy bài viết hay và hữu ích, hãy để lại 1 like, share, đánh giá để ủng hộ đội ngũ biên tập của hoccode.org nhé!
Chúc các bạn thực hiện thành công!