TextEncoder

TextEncoder takes a stream of code points as input and emits a stream of UTF-8 bytes.

注意 : There is a polyfill implementation to support non-UTF-8 text encodings on GitHub .

范例

const encoder = new TextEncoder()
const view = encoder.encode('€')
console.log(view); // Uint8Array(3) [226, 130, 172]
					

构造函数

TextEncoder()
Returns a newly constructed TextEncoder that will generate a byte stream with UTF-8 encoding.

特性

TextEncoder interface doesn't inherit any property.

TextEncoder.prototype.encoding 只读
Always returns " utf-8 ".

方法

TextEncoder interface doesn't inherit any method .

TextEncoder.prototype.encode()
Takes a USVString as input, and returns a Uint8Array containing UTF-8 encoded text.
TextEncoder.prototype.encodeInto()
Takes a USVString to encode and a destination Uint8Array to put resulting UTF-8 encoded text into, and returns a dictionary object indicating the progress of the encoding. This is potentially more performant than the older encode() 方法。

Polyfill

The below polyfill is compliant with the standard and therefore only supports UTF-8. It is designed to work in IE5 "out of the box". However, in IE5-IE9, it will return a regular Array instead of a TypedArray. In those cases a polyfill might be impractical for large strings. Finally, note that you should run the below code through a minifier (especially closure compiler) to turn sequences like 0x1e << 3 into 0xf0 . These sequences are not already precomputed because they serve to aesthetically illustrate how the polyfill works.

if (typeof TextEncoder === "undefined") {
    TextEncoder=function TextEncoder(){};
    TextEncoder.prototype.encode = function encode(str) {
        "use strict";
        var Len = str.length, resPos = -1;
        // The Uint8Array's length must be at least 3x the length of the string because an invalid UTF-16
        //  takes up the equivelent space of 3 UTF-8 characters to encode it properly. However, Array's
        //  have an auto expanding length and 1.5x should be just the right balance for most uses.
        var resArr = typeof Uint8Array === "undefined" ? new Array(Len * 1.5) : new Uint8Array(Len * 3);
        for (var point=0, nextcode=0, i = 0; i !== Len; ) {
            point = str.charCodeAt(i), i += 1;
            if (point >= 0xD800 && point <= 0xDBFF) {
                if (i === Len) {
                    resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/;
                    resArr[resPos += 1] = 0xbd/*0b10111101*/; break;
                }
                // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
                nextcode = str.charCodeAt(i);
                if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) {
                    point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000;
                    i += 1;
                    if (point > 0xffff) {
                        resArr[resPos += 1] = (0x1e/*0b11110*/<<3) | (point>>>18);
                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>12)&0x3f/*0b00111111*/);
                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>6)&0x3f/*0b00111111*/);
                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/);
                        continue;
                    }
                } else {
                    resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/;
                    resArr[resPos += 1] = 0xbd/*0b10111101*/; continue;
                }
            }
            if (point <= 0x007f) {
                resArr[resPos += 1] = (0x0/*0b0*/<<7) | point;
            } else if (point <= 0x07ff) {
                resArr[resPos += 1] = (0x6/*0b110*/<<5) | (point>>>6);
                resArr[resPos += 1] = (0x2/*0b10*/<<6)  | (point&0x3f/*0b00111111*/);
            } else {
                resArr[resPos += 1] = (0xe/*0b1110*/<<4) | (point>>>12);
                resArr[resPos += 1] = (0x2/*0b10*/<<6)    | ((point>>>6)&0x3f/*0b00111111*/);
                resArr[resPos += 1] = (0x2/*0b10*/<<6)    | (point&0x3f/*0b00111111*/);
            }
        }
        if (typeof Uint8Array !== "undefined") return resArr.subarray(0, resPos + 1);
        // else // IE 6-9
        resArr.length = resPos + 1; // trim off extra weight
        return resArr;
    };
    TextEncoder.prototype.toString = function(){return "[object TextEncoder]"};
    try { // Object.defineProperty only works on DOM prototypes in IE8
        Object.defineProperty(TextEncoder.prototype,"encoding",{
            get:function(){if(TextEncoder.prototype.isPrototypeOf(this)) return"utf-8";
                           else throw TypeError("Illegal invocation");}
        });
    } catch(e) { /*IE6-8 fallback*/ TextEncoder.prototype.encoding = "utf-8"; }
    if(typeof Symbol!=="undefined")TextEncoder.prototype[Symbol.toStringTag]="TextEncoder";
}
					

Source: https://github.com/anonyco/FastestSmallestTextEncoderDecoder

规范

规范 状态 Comment
编码
The definition of 'TextEncoder' in that specification.
实时标准 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
TextEncoder
Chrome 38 Edge 79 Firefox 19
19
18
注意事项
Firefox 18 implemented an earlier and slightly different version of the specification.
IE 不支持 No Opera 25 Safari 10.1 WebView Android 38 Chrome Android 38 Firefox Android 19
19
18
注意事项
Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android Yes Safari iOS 10.3 Samsung Internet Android 3.0
TextEncoder() 构造函数
Chrome 53
注意事项
53
注意事项
Does not accept parameters. Supports only utf-8 encoding.
38 — 53
注意事项
Throws RangeError exception for unknown encoding types.
Edge 79
注意事项
79
注意事项
Does not accept parameters. Supports only utf-8 encoding.
Firefox 48
注意事项
48
注意事项
The constructor accepts an encoding type label argument, but the value is ignored. Only utf-8 encoding is supported.
38 — 48
注意事项
If the encoding type label argument is invalid, then a RangeError exception is thrown.
19 — 38
注意事项
If the encoding type label argument is invalid, then a TypeError exception is thrown.
18
注意事项
Firefox 18 implemented an earlier and slightly different version of the specification.
IE 不支持 No Opera 25 Safari 10.1 WebView Android 38 Chrome Android 38 Firefox Android 48
注意事项
48
注意事项
The constructor accepts an encoding type label argument, but the value is ignored. Only utf-8 encoding is supported.
38 — 48
注意事项
If the encoding type label argument is invalid, then a RangeError exception is thrown.
19 — 38
注意事项
If the encoding type label argument is invalid, then a TypeError exception is thrown.
18
注意事项
Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS 10.3 Samsung Internet Android 3.0
encode
Chrome 38 Edge 79 Firefox 19
19
18
注意事项
Firefox 18 implemented an earlier and slightly different version of the specification.
IE 不支持 No Opera 25 Safari 10.1 WebView Android 38 Chrome Android 38 Firefox Android 19
19
18
注意事项
Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android Yes Safari iOS 10.3 Samsung Internet Android 3.0
encodeInto
Chrome 74 Edge 79 Firefox 66 IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 74 Chrome Android 74 Firefox Android 66 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 11.0
encoding
Chrome 38 Edge 79 Firefox 19
19
18
注意事项
Firefox 18 implemented an earlier and slightly different version of the specification.
IE 不支持 No Opera 25 Safari 10.1 WebView Android 38 Chrome Android 38 Firefox Android 19
19
18
注意事项
Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android Yes Safari iOS 10.3 Samsung Internet Android 3.0
Available in workers
Chrome 38 Edge 79 Firefox 20 IE 不支持 No Opera 25 Safari 10.1 WebView Android 38 Chrome Android 38 Firefox Android 20 Opera Android ? Safari iOS 10.3 Samsung Internet Android 3.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。

另请参阅

  1. 编码 API
  2. TextEncoder
  3. 构造函数
    1. TextEncoder()
  4. 特性
    1. encoding
  5. 方法
    1. encode()
    2. encodeInto()
  6. Related pages for Encoding API
    1. TextDecoder