TypedArray.of()
The TypedArray.of()
method creates a new typed
array from a variable number of arguments. This method is nearly the same as
Array.of()
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
TypedArray.of(element0[, element1[, ...[, elementN]]])
Where TypedArray
is one of:
Parameters
elementN
- Elements of which to create the typed array.
Return value
A new TypedArray
instance.
Description
Some subtle distinctions between Array.of()
and
TypedArray.of()
:
- If the
this
value passed toTypedArray.of()
is not a constructor,TypedArray.of()
will throw aTypeError
, whereArray.of()
defaults to creating a newArray
. TypedArray.of()
uses[[Put]]
whereArray.of()
uses[[DefineProperty]]
. Hence, when working withProxy
objects, it callshandler.set
to create new elements rather thanhandler.defineProperty()
.
Examples
Using of()
Uint8Array.of(1); // Uint8Array [ 1 ]
Int8Array.of('1', '2', '3'); // Int8Array [ 1, 2, 3 ]
Float32Array.of(1, 2, 3); // Float32Array [ 1, 2, 3 ]
Int16Array.of(undefined); // Int16Array [ 0 ]
Specifications
Browser compatibility
BCD tables only load in the browser