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

js
decode()
decode(buffer)
decode(buffer, options)

Tham số

buffer Optional

Một đối tượng ArrayBuffer, TypedArray, hoặc DataView chứa văn bản đã mã hóa cần giải mã.

options Optional

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ành true nếu xử lý dữ liệu theo từng khối, và false cho 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.fataltrue.

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

html
<p>Encoded value: <span id="encoded-value"></span></p>
<p>Decoded value: <span id="decoded-value"></span></p>

JavaScript

js
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

Thông số kỹ thuật
Encoding
# ref-for-dom-textdecoder-decode①

Tương thích trình duyệt

Xem thêm