Encoding API
TextDecoder() 构造函数使用参数中指定的编码返回一个新创建的 TextDecoder 对象。
语法
new TextDecoder()
new TextDecoder(utfLabel)
new TextDecoder(utfLabel, options)
参数
utfLabel- : 一个字符串,默认是
"utf-8"。可以是任意有效的编码。
- : 一个字符串,默认是
options- : 一个具有属性的对象:
fatal- : 一个布尔值,表示在解码无效数据时,TextDecoder.decode() 方法是否必须抛出 TypeError。默认是
false,这意味着解码器将用替换字符替换错误的数据。
- : 一个布尔值,表示在解码无效数据时,TextDecoder.decode() 方法是否必须抛出 TypeError。默认是
- : 一个具有属性的对象:
异常
- RangeError
- : 如果
label值是未知的,或是使用了'replacement'解码算法("iso-2022-cn"或"iso-2022-cn-ext"两个值之一),则会抛出。
- : 如果
示例
const textDecoder1 = new TextDecoder("iso-8859-2");
const textDecoder2 = new TextDecoder();
const textDecoder3 = new TextDecoder("csiso2022kr", { fatal: true }); // Allows TypeError exception to be thrown.
const textDecoder4 = new TextDecoder("iso-2022-cn"); // Throw a RangeError exception.
规范
浏览器兼容性
参见
- 属于 TextDecoder 接口。
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
