Uint16Array() Konstruktor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Der Uint16Array() Konstruktor erstellt Uint16Array Objekte. Der Inhalt wird auf 0 initialisiert, es sei denn, es werden explizit Initialisierungsdaten bereitgestellt.

Syntax

js
new Uint16Array()
new Uint16Array(length)
new Uint16Array(typedArray)
new Uint16Array(object)

new Uint16Array(buffer)
new Uint16Array(buffer, byteOffset)
new Uint16Array(buffer, byteOffset, length)

Note: Uint16Array() kann nur mit new konstruiert werden. Der Versuch, es ohne new aufzurufen, führt zu einem TypeError.

Parameter

Siehe TypedArray.

Ausnahmen

Siehe TypedArray.

Beispiele

Verschiedene Möglichkeiten, ein Uint16Array zu erstellen

js
// From a length
const uint16 = new Uint16Array(2);
uint16[0] = 42;
console.log(uint16[0]); // 42
console.log(uint16.length); // 2
console.log(uint16.BYTES_PER_ELEMENT); // 2

// From an array
const x = new Uint16Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
const y = new Uint16Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(16);
const z = new Uint16Array(buffer, 2, 4);
console.log(z.byteOffset); // 2

// From an iterable
const iterable = (function* () {
  yield* [1, 2, 3];
})();
const uint16FromIterable = new Uint16Array(iterable);
console.log(uint16FromIterable);
// Uint16Array [1, 2, 3]

Spezifikationen

Specification
ECMAScript Language Specification
# sec-typedarray-constructors

Browser-Kompatibilität

BCD tables only load in the browser

Siehe auch