Hướng dẫn dùng typeerror: JavaScript

The 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.

A TypeError may be thrown when:

  • an operand or argument passed to a function is incompatible with the type expected by that operator or function; or
  • when attempting to modify a value that cannot be changed; or
  • when attempting to use a value in an inappropriate way.

TypeError is a serializable object, so it can be cloned with structuredClone[] or copied between Workers using postMessage[].

Constructor

TypeError[]

Creates a new TypeError object.

Instance properties

TypeError.prototype.message

Error message. Inherited from Error.

TypeError.prototype.name

Error name. Inherited from Error.

TypeError.prototype.cause

Error cause. Inherited from Error.

TypeError.prototype.fileName Non-standard

Path to file that raised this error. Inherited from Error.

TypeError.prototype.lineNumber Non-standard

Line number in file that raised this error. Inherited from Error.

TypeError.prototype.columnNumber Non-standard

Column number in line that raised this error. Inherited from Error.

TypeError.prototype.stack Non-standard

Stack 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

Specification
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ằng

Hướ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

...
const KEY = '123'; // Dùng hằng
KEY = document.getElementById["key-input"].value;
...

...
var KEY = '123'; // Dùng biến
KEY = document.getElementById["key-input"].value;
...
Tình huống 2: Việc dữ liệu của Hằng [Constant] bị thay đổi là do bạn vô ý. Việc thay đổi là không cần thiết.

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!

Chủ Đề