TextDecoder class

WHATWG 编码标准TextDecoder API 的实现。

const decoder = new TextDecoder();
const u8arr = new Uint8Array([72, 101, 108, 108, 111]);
console.log(decoder.decode(u8arr)); // Hello

构造函数

TextDecoder(string, { fatal?: boolean, ignoreBOM?: boolean })

属性

encoding

TextDecoder 实例支持的编码。

fatal

如果解码错误导致引发 true,该值将 TypeError

ignoreBOM

如果解码结果将包含字节顺序标记,则该值将 true

方法

decode(null | ArrayBuffer | ArrayBufferView, { stream?: boolean })

解码 input 并返回字符串。 如果 options.streamtrue,则 input 末尾发生的任何不完整的字节序列在内部缓冲,并在下一次调用 textDecoder.decode()后发出。

如果 textDecoder.fataltrue,则发生的解码错误将导致引发 TypeError

构造函数详细信息

TextDecoder(string, { fatal?: boolean, ignoreBOM?: boolean })

new TextDecoder(encoding?: string, options?: { fatal?: boolean, ignoreBOM?: boolean })

参数

encoding

string

options

{ fatal?: boolean, ignoreBOM?: boolean }

属性详细信息

encoding

TextDecoder 实例支持的编码。

encoding: string

属性值

string

fatal

如果解码错误导致引发 true,该值将 TypeError

fatal: boolean

属性值

boolean

ignoreBOM

如果解码结果将包含字节顺序标记,则该值将 true

ignoreBOM: boolean

属性值

boolean

方法详细信息

decode(null | ArrayBuffer | ArrayBufferView, { stream?: boolean })

解码 input 并返回字符串。 如果 options.streamtrue,则 input 末尾发生的任何不完整的字节序列在内部缓冲,并在下一次调用 textDecoder.decode()后发出。

如果 textDecoder.fataltrue,则发生的解码错误将导致引发 TypeError

function decode(input?: null | ArrayBuffer | ArrayBufferView, options?: { stream?: boolean }): string

参数

input

null | ArrayBuffer | ArrayBufferView

包含编码数据的 ArrayBufferDataViewTypedArray 实例。

options

{ stream?: boolean }

返回

string