TextEncoder
ClassEncodes strings into UTF-8 byte arrays.
Index
Properties
Methods
Example
// Encode a string to UTF-8 bytes with TextEncoder
const encoder = new TextEncoder();
const encoded = encoder.encode('Hello World');
console.log('Encoded bytes length:', encoded.length);
console.log('Encoder encoding:', encoder.encoding);
// Decode bytes back to string with TextDecoder
const decoder = new TextDecoder();
const decoded = decoder.decode(encoded);
console.log('Decoded string:', decoded);
console.log('Decoder encoding:', decoder.encoding);
// Verify round-trip: encode then decode produces the original string
const original = 'Lens Studio Editor API';
const roundTrip = new TextDecoder().decode(new TextEncoder().encode(original));
console.log('Round-trip match:', original === roundTrip);
// encodeInto writes directly into an existing Uint8Array
const buffer = new Uint8Array(32);
encoder.encodeInto('Test', buffer);
console.log('encodeInto first byte (T = 84):', buffer[0]);
Extends
Constructors
Constructor
new TextEncoder():
TextEncoder
Creates a new TextEncoder instance.
Returns
TextEncoder
Overrides
ScriptObject.constructor
Properties
encoding
readonlyencoding:string
The encoding format used by this encoder, always 'utf-8'.
Methods
encode()
encode(
value):Uint8Array
Encodes a string into a Uint8Array using UTF-8.
Parameters
| Parameter | Type |
|---|---|
value | string |
Returns
Uint8Array
encodeInto()
encodeInto(
value,result):void
Encodes a string into an existing Uint8Array in place.
Parameters
| Parameter | Type |
|---|---|
value | string |
result | Uint8Array |
Returns
void
getTypeName()
staticgetTypeName():string
Returns the type name of this class.
Returns
string
Overrides
getTypeName()
getTypeName():
string
Returns the name of this object's type.
Returns
string
Inherited from
isOfType()
isOfType(
type):boolean
Returns true if the object is of the specified type.
Parameters
| Parameter | Type |
|---|---|
type | string |
Returns
boolean
Inherited from
isSame()
isSame(
other):boolean
Returns true if this object refers to the same instance as the given object.
Parameters
| Parameter | Type |
|---|---|
other | ScriptObject |
Returns
boolean