A
TypedArray
object describes an array-like view of an underlying
binary data buffer
. There is no global property named
TypedArray
, nor is there a directly visible
TypedArray
constructor. Instead, there are a number of different global properties, whose values are typed array constructors for specific element types, listed below. On the following pages you will find common properties and methods that can be used with any typed array containing elements of any type.
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.
ECMAScript 2015 defines a
TypedArray
constructor that serves as the
[[Prototype]]
of all
TypedArray
constructors. This constructor is not directly exposed: there is no global
%TypedArray%
or
TypedArray
property. It is only directly accessible through
Object.getPrototypeOf(Int8Array)
and similar. All
TypedArray
s constructors inherit common properties from the
%TypedArray%
constructor function. Additionally, all typed array prototypes (
TypedArray.
prototype
) have
%TypedArray%.prototype
as their
[[Prototype]]
.
%TypedArray%
constructor on its own is not particularly useful. Calling it or using it in a
new
expression will throw a
TypeError
, except when used during object creation in JS engines that support subclassing. There are at present no such engines, so
%TypedArray%
is only useful to polyfill functions or properties onto all
TypedArray
构造函数。
When creating an instance of a
TypedArray
(e.g.
Int8Array
), an array buffer is created internally in memory or, if an
ArrayBuffer
object is given as constructor argument, then this is used instead. The buffer address is saved as an internal property of the instance and all the methods of
%
TypedArray
%.prototype
, i.e. set value and get value etc., operate on that array buffer address.
| Type | 值范围 | Size in bytes | 描述 | Web IDL 类型 | Equivalent C type |
|---|---|---|---|---|---|
Int8Array
|
-128
to
127
|
1 | 8-bit two's complement signed integer |
byte
|
int8_t
|
Uint8Array
|
0
to
255
|
1 | 8-bit unsigned integer |
octet
|
uint8_t
|
Uint8ClampedArray
|
0
to
255
|
1 | 8-bit unsigned integer (clamped) |
octet
|
uint8_t
|
Int16Array
|
-32768
to
32767
|
2 | 16-bit two's complement signed integer |
short
|
int16_t
|
Uint16Array
|
0
to
65535
|
2 | 16-bit unsigned integer |
unsigned short
|
uint16_t
|
Int32Array
|
-2147483648
to
2147483647
|
4 | 32-bit two's complement signed integer |
long
|
int32_t
|
Uint32Array
|
0
to
4294967295
|
4 | 32-bit unsigned integer |
unsigned long
|
uint32_t
|
Float32Array
|
1.2
×
10
-38
to
3.4
×
10
38
|
4 |
32-bit IEEE floating point number (7 significant digits e.g.,
1.1234567
)
|
unrestricted float
|
float
|
Float64Array
|
5.0
×
10
-324
to
1.8
×
10
308
|
8 |
64-bit IEEE floating point number (16 significant digits e.g.,
1.123...15
)
|
unrestricted double
|
double
|
BigInt64Array
|
-2
63
to
2
63
-1
|
8 | 64-bit two's complement signed integer |
bigint
|
int64_t (signed long long)
|
BigUint64Array
|
0
to
2
64
-1
|
8 | 64-bit unsigned integer |
bigint
|
uint64_t (unsigned long long)
|
This object cannot be instantiated directly. Instead, you create an instance of an array of a particular type, such as a
Int8Array
或
BigInt64Array
. These objects all have a common syntax for their constructors:
new TypedArray(); new TypedArray(length); new TypedArray(typedArray); new TypedArray(object); new TypedArray(buffer [, byteOffset [, length]]);
Where TypedArray is a constructor for one of the concrete types.
length
length
argument, an internal array buffer is created in memory, of size
length
multiplied by
BYTES_PER_ELEMENT
bytes, containing zeros.
typedArray
typedArray
argument, which can be an object of any of the typed array types (such as
Int32Array
), the
typedArray
gets copied into a new typed array. Each value in
typedArray
is converted to the corresponding type of the constructor before being copied into the new array. The length of the new typed array will be same as the length of the
typedArray
自变量。
object
object
argument, a new typed array is created as if by the
TypedArray
.from()
方法。
buffer
,
byteOffset
,
length
buffer
, and optionally a
byteOffset
和
length
argument, a new typed array view is created that views the specified
ArrayBuffer
。
byteOffset
and
length
parameters specify the memory range that will be exposed by the typed array view. If both are omitted, all of
buffer
is viewed; if only
length
is omitted, the remainder of
buffer
is viewed.
TypedArray.BYTES_PER_ELEMENT
TypedArray
对象。
TypedArray.name
"Int8Array"
).
get TypedArray[@@species]
The constructor function used to create derived objects.
TypedArray.prototype
TypedArray
对象。
TypedArray.from()
TypedArray
from an array-like or iterable object. See also
Array.from()
.
TypedArray.of()
TypedArray
with a variable number of arguments. See also
Array.of()
.
TypedArray.prototype.buffer
ArrayBuffer
referenced by the typed array. Fixed at construction time and thus
read only
.
TypedArray.prototype.byteLength
TypedArray.prototype.byteOffset
ArrayBuffer
. Fixed at construction time and thus
read only.
TypedArray.prototype.length
TypedArray.prototype.copyWithin()
Array.prototype.copyWithin()
.
TypedArray.prototype.entries()
Array Iterator
object that contains the key/value pairs for each index in the array. See also
Array.prototype.entries()
.
TypedArray.prototype.every()
Array.prototype.every()
.
TypedArray.prototype.fill()
Array.prototype.fill()
.
TypedArray.prototype.filter()
true
。另请参阅
Array.prototype.filter()
.
TypedArray.prototype.find()
undefined
if not found. See also
Array.prototype.find()
.
TypedArray.prototype.findIndex()
-1
if not found. See also
Array.prototype.findIndex()
.
TypedArray.prototype.forEach()
Array.prototype.forEach()
.
TypedArray.prototype.includes()
true
or
false
as appropriate. See also
Array.prototype.includes()
.
TypedArray.prototype.indexOf()
-1
if none is found. See also
Array.prototype.indexOf()
.
TypedArray.prototype.join()
Array.prototype.join()
.
TypedArray.prototype.keys()
Array Iterator
that contains the keys for each index in the array. See also
Array.prototype.keys()
.
TypedArray.prototype.lastIndexOf()
-1
if none is found. See also
Array.prototype.lastIndexOf()
.
TypedArray.prototype.map()
Array.prototype.map()
.
TypedArray.prototype.reduce()
Array.prototype.reduce()
.
TypedArray.prototype.reduceRight()
Array.prototype.reduceRight()
.
TypedArray.prototype.reverse()
Array.prototype.reverse()
.
TypedArray.prototype.set()
Stores multiple values in the typed array, reading input values from a specified array.
TypedArray.prototype.slice()
Array.prototype.slice()
.
TypedArray.prototype.some()
true
if at least one element in this array satisfies the provided testing function. See also
Array.prototype.some()
.
TypedArray.prototype.sort()
Array.prototype.sort()
.
TypedArray.prototype.subarray()
TypedArray
from the given start and end element index.
TypedArray.prototype.values()
Array Iterator
object that contains the values for each index in the array. See also
Array.prototype.values()
.
TypedArray.prototype.toLocaleString()
Array.prototype.toLocaleString()
.
TypedArray.prototype.toString()
Array.prototype.toString()
.
TypedArray.prototype[@@iterator]()
Array Iterator
object that contains the values for each index in the array.
Starting with ECMAScript 2015,
TypedArray
constructors must be constructed with the
new
operator. Calling a
TypedArray
constructor as a function without
new
will throw a
TypeError
.
var dv = Int8Array([1, 2, 3]); // TypeError: calling a builtin Int8Array constructor // without new is forbidden
var dv = new Int8Array([1, 2, 3]);
You can reference elements in the array using standard array index syntax (that is, using bracket notation). However, getting or setting indexed properties on typed arrays will not search in the prototype chain for this property, even when the indices are out of bound. Indexed properties will consult the
ArrayBuffer
and will never look at object properties. You can still use named properties, just like with all objects.
// Setting and getting using standard array syntax var int16 = new Int16Array(2); int16[0] = 42; console.log(int16[0]); // 42 // Indexed properties on prototypes are not consulted (Fx 25) Int8Array.prototype[20] = 'foo'; (new Int8Array(32))[20]; // 0 // even when out of bound Int8Array.prototype[20] = 'foo'; (new Int8Array(8))[20]; // undefined // or with negative integers Int8Array.prototype[-1] = 'foo'; (new Int8Array(8))[-1]; // undefined // Named properties are allowed, though (Fx 30) Int8Array.prototype.foo = 'bar'; (new Int8Array(32)).foo; // "bar"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'TypedArray Objects' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TypedArray
|
Chrome 7 | Edge 12 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.10 |
BYTES_PER_ELEMENT
|
Chrome 7 | Edge 12 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.10 |
buffer
|
Chrome 7 | Edge 14 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.10 |
byteLength
|
Chrome 7 | Edge 14 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.10 |
byteOffset
|
Chrome 7 | Edge 14 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.10 |
| Constructor without arguments | Chrome 7 | Edge 12 | Firefox 55 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 55 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs 0.10 |
copyWithin
|
Chrome 45 | Edge 14 | Firefox 34 | IE No | Opera 36 | Safari 9.1 | WebView Android No | Chrome Android No | Firefox Android 34 | Opera Android No | Safari iOS 9.3 | Samsung Internet Android No | nodejs 4.0.0 |
entries
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 36 | Safari No | WebView Android No | Chrome Android 45 | Firefox Android 37 | Opera Android No | Safari iOS No | Samsung Internet Android 5.0 | nodejs 0.12 |
every
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 36 | Safari No | WebView Android No | Chrome Android 45 | Firefox Android 37 | Opera Android No | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
fill
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 36 | Safari No | WebView Android No | Chrome Android 45 | Firefox Android 37 | Opera Android No | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
filter
|
Chrome 45 | Edge 14 | Firefox 38 | IE No | Opera No | Safari No | WebView Android No | Chrome Android 45 | Firefox Android 38 | Opera Android No | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
find
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 32 | Safari No | WebView Android 45 | Chrome Android 45 | Firefox Android 37 | Opera Android 32 | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
findIndex
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 32 | Safari No | WebView Android 45 | Chrome Android 45 | Firefox Android 37 | Opera Android 32 | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
forEach
|
Chrome 45 | Edge 14 | Firefox 38 | IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 38 | Opera Android 32 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
from
|
Chrome 45 | Edge 14 | Firefox 38 | IE No | Opera No | Safari 10 | WebView Android No | Chrome Android No | Firefox Android 38 | Opera Android No | Safari iOS 10 | Samsung Internet Android No | nodejs 4.0.0 |
includes
|
Chrome 47 | Edge 14 | Firefox 43 | IE No | Opera 34 | Safari 10 | WebView Android No | Chrome Android 47 | Firefox Android 43 | Opera Android 34 | Safari iOS 10 | Samsung Internet Android 5.0 |
nodejs
6.0.0
|
| Indexed properties not consulting prototype |
Chrome
7
|
Edge
12
|
Firefox 25 |
IE
10
|
Opera
11.6
|
Safari
5.1
|
WebView Android
≤37
|
Chrome Android
18
|
Firefox Android 25 |
Opera Android
12
|
Safari iOS
5
|
Samsung Internet Android
1.0
|
nodejs
0.10
|
indexOf
|
Chrome 45 | Edge 14 |
Firefox
37
|
IE No | Opera 32 | Safari No | WebView Android No | Chrome Android 45 |
Firefox Android
37
|
Opera Android 32 | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
| Iterable in constructor | Chrome 39 | Edge 14 | Firefox 52 | IE No | Opera 26 | Safari 10 | WebView Android 39 | Chrome Android 39 | Firefox Android 52 | Opera Android 26 | Safari iOS 10 | Samsung Internet Android 4.0 | nodejs 4.0.0 |
join
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 32 | Safari No | WebView Android 45 | Chrome Android 45 | Firefox Android 37 | Opera Android 32 | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
keys
|
Chrome 38 | Edge 14 | Firefox 37 | IE No | Opera 25 | Safari 10 | WebView Android 38 | Chrome Android 38 | Firefox Android 37 | Opera Android 25 | Safari iOS 10 | Samsung Internet Android 3.0 | nodejs 0.12 |
lastIndexOf
|
Chrome 45 | Edge 14 |
Firefox
37
|
IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 |
Firefox Android
37
|
Opera Android 32 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
length
|
Chrome 7 | Edge 14 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.10 |
map
|
Chrome 45 | Edge 14 | Firefox 38 | IE No | Opera 32 | Safari No | WebView Android 45 | Chrome Android 45 | Firefox Android 38 | Opera Android 32 | Safari iOS No | Samsung Internet Android 5.0 | nodejs 4.0.0 |
name
|
Chrome 7 | Edge 12 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.10 |
| Named properties | Chrome 7 | Edge 12 | Firefox 30 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 30 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs 0.10 |
TypedArray()
without
new
throws
|
Chrome 7 | Edge 14 | Firefox 44 | IE No | Opera 15 | Safari 5.1 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 44 | Opera Android 14 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs 0.12 |
of
|
Chrome 45 | Edge 14 | Firefox 38 | IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 38 | Opera Android No | Safari iOS No | Samsung Internet Android No | nodejs 4.0.0 |
reduce
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 37 | Opera Android No | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
reduceRight
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 37 | Opera Android No | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
reverse
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 37 | Opera Android No | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
set
|
Chrome 7 | Edge 14 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.10 |
slice
|
Chrome 45 | Edge 14 | Firefox 38 | IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 38 | Opera Android 32 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
some
|
Chrome 45 | Edge 14 | Firefox 37 | IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 37 | Opera Android No | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
sort
|
Chrome 45 | Edge 14 | Firefox 46 | IE No | Opera 32 | Safari 10 | WebView Android 45 | Chrome Android 45 | Firefox Android 46 | Opera Android 32 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
subarray
|
Chrome 7 | Edge 14 | Firefox 4 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 4.2 | Samsung Internet Android 1.0 | nodejs 0.12 |
toLocaleString
|
Chrome 7 | Edge 12 | Firefox 51 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 51 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs 0.10 |
toString
|
Chrome 7 | Edge 12 | Firefox 51 | IE 10 | Opera 11.6 | Safari 5.1 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 51 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs 0.10 |
值
|
Chrome 38 | Edge 14 | Firefox 37 | IE No | Opera 25 | Safari 10 | WebView Android 38 | Chrome Android 38 | Firefox Android 37 | Opera Android 25 | Safari iOS 10 | Samsung Internet Android 3.0 | nodejs 0.12 |
@@iterator
|
Chrome 38 | Edge 12 |
Firefox
36
|
IE No | Opera 25 | Safari 10 | WebView Android 38 | Chrome Android 38 |
Firefox Android
36
|
Opera Android 25 | Safari iOS 10 | Samsung Internet Android 3.0 | nodejs 0.12 |
@@species
|
Chrome 51 | Edge 13 | Firefox 48 | IE No | Opera 38 | Safari No | WebView Android 51 | Chrome Android 51 | Firefox Android 48 | Opera Android 41 | Safari iOS No | Samsung Internet Android 5.0 |
nodejs
6.5.0
|
完整支持
不支持
见实现注意事项。
用户必须明确启用此特征。
使用非标名称。
ArrayBuffer
DataView
TypedArray
TypedArray.from()
TypedArray.of()
TypedArray.prototype.copyWithin()
TypedArray.prototype.entries()
TypedArray.prototype.every()
TypedArray.prototype.fill()
TypedArray.prototype.filter()
TypedArray.prototype.find()
TypedArray.prototype.findIndex()
TypedArray.prototype.forEach()
TypedArray.prototype.includes()
TypedArray.prototype.indexOf()
TypedArray.prototype.join()
TypedArray.prototype.keys()
TypedArray.prototype.lastIndexOf()
TypedArray.prototype.map()
TypedArray.prototype.reduce()
TypedArray.prototype.reduceRight()
TypedArray.prototype.reverse()
TypedArray.prototype.set()
TypedArray.prototype.slice()
TypedArray.prototype.some()
TypedArray.prototype.sort()
TypedArray.prototype.subarray()
TypedArray.prototype.toLocaleString()
TypedArray.prototype.toString()
TypedArray.prototype.values()
TypedArray.prototype[@@iterator]()
Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
BigInt64Array
BigUint64Array
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()