TextEncoder
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.
构造函数
TextEncoder()
返回一个新构造的
TextEncoder
,它默认使用 UTF-8 编码将码位流转换成字节流。
属性
TextEncoder
接口不继承任何属性。
TextEncoder..encoding
只读总是返回
utf-8
。
方法
TextEncoder
接口不继承任何方法。
TextEncoder.encode()
接受一个字符串作为输入,返回一个包含 UTF-8 编码的文本的
Uint8Array
。TextEncoder.encodeInto()
接受一个字符串(编码的对象)和一个目标
Uint8Array
(用于放入生成的 UTF-8 编码的文本)作为输入,并且返回一个指示编码进度的对象。此方法的性能可能会比更早出现的encode()
方法好一些。
示例
const encoder = new TextEncoder();
const view = encoder.encode("€");
console.log(view); // Uint8Array(3) [226, 130, 172]
规范
Specification |
---|
Encoding # interface-textencoder |
浏览器兼容性
参见
TextDecoder
接口描述了逆操作。- Node.js 从 v11.0.0 开始支持全局导出