TextDecoder: decode() method
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
Note: This feature is available in Web Workers.
Phương thức TextDecoder.decode() trả về một chuỗi chứa văn bản đã được giải mã từ bộ đệm được truyền vào làm tham số.
Phương thức giải mã được xác định bởi đối tượng TextDecoder hiện tại.
Điều này bao gồm mã hóa mong đợi của dữ liệu, và cách xử lý lỗi khi giải mã.
Cú pháp
decode()
decode(buffer)
decode(buffer, options)
Tham số
bufferOptional-
Một đối tượng
ArrayBuffer,TypedArray, hoặcDataViewchứa văn bản đã mã hóa cần giải mã. optionsOptional-
Một đối tượng với thuộc tính:
stream-
Một cờ boolean cho biết có dữ liệu bổ sung sẽ tiếp tục xuất hiện trong các lần gọi
decode()tiếp theo hay không. Đặt thànhtruenếu xử lý dữ liệu theo từng khối, vàfalsecho khối cuối cùng hoặc nếu dữ liệu không được chia khối. Mặc định làfalse.
Ngoại lệ
TypeError-
Được ném ra nếu xảy ra lỗi giải mã khi thuộc tính
TextDecoder.fatallàtrue.
Giá trị trả về
Một chuỗi.
Ví dụ
Ví dụ này mã hóa và giải mã ký hiệu euro, €.
HTML
<p>Encoded value: <span id="encoded-value"></span></p>
<p>Decoded value: <span id="decoded-value"></span></p>
JavaScript
const encoder = new TextEncoder();
const array = encoder.encode("€"); // Uint8Array(3) [226, 130, 172]
document.getElementById("encoded-value").textContent = array;
const decoder = new TextDecoder();
const str = decoder.decode(array); // String "€"
document.getElementById("decoded-value").textContent = str;
Kết quả
Thông số kỹ thuật
| Specification |
|---|
| Encoding> # ref-for-dom-textdecoder-decode①> |
Tương thích trình duyệt
Xem thêm
- Giao diện
TextDecodermà nó thuộc về.