TypedArray

翻譯不完整。 請幫助我們翻譯這篇文章!

TypedArray 物件表示了一個底層 ArrayBuffer 的類陣列(array-like)視圖,它能以限定的型別解讀、修改 ArrayBuffer。但並沒有名為 TypedArray 的內建物件,TypedArray 也不存在可直接呼叫的建構式。真正能夠使用的是幾個原型繼承自 TypedArray 的內建物件,它們可以建立限定成員型別的物件實體來操作 ArrayBuffer。這些 TypedArray 型別的物件僅為視圖,並不會存放資料,所有的資料皆實際儲存於 ArrayBuffer 物件當中。以下將說明每種限定成員型別之 TypedArray 的共同屬性與方法。

語法

new TypedArray(length);
new TypedArray(typedArray);
new TypedArray(object);
new TypedArray(buffer [, byteOffset [, length]]);

where TypedArray() is one of:

Int8Array();
Uint8Array();
Uint8ClampedArray();
Int16Array();
Uint16Array();
Int32Array();
Uint32Array();
Float32Array();
Float64Array();

參數

length
When called with a length argument, a typed array containing length zeroes is created.
typedArray
When called with a 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.
object
When called with an object argument, a new typed array is created as if by the TypedArray.from() method.
buffer, byteOffset, length
When called with a buffer, and optionally a byteOffset and a length argument, a new typed array view is created that views the specified ArrayBuffer. The 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.

說明

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 TypedArrays constructors inherit common properties from the %TypedArray% constructor function.  Additionally, all typed array prototypes (TypedArray.prototype) have %TypedArray%.prototype as their [[Prototype]].

The %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 constructors.

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.

Property access

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"

TypedArray 物件

Type Value Range Size in bytes Description Web IDL type 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.2x10-38 to 3.4x1038 4 32-bit IEEE floating point number ( 7 significant digits e.g. 1.1234567) unrestricted float float
Float64Array 5.0x10-324 to 1.8x10308 8 64-bit IEEE floating point number (16 significant digits e.g. 1.123...15) unrestricted double double

屬性

TypedArray.BYTES_PER_ELEMENT
Returns a number value of the element size for the different typed array objects.
TypedArray.length
Length property whose value is 0.
TypedArray.name
Returns the string value of the constructor name. E.g "Int8Array".
get TypedArray[@@species]
The constructor function that is used to create derived objects.
TypedArray.prototype
Prototype for the TypedArray objects.

方法

TypedArray.from()
Creates a new typed array from an array-like or iterable object. See also Array.from().
TypedArray.of()
Creates a new typed array with a variable number of arguments. See also Array.of().

TypedArray 原型

All TypedArrays inherit from TypedArray.prototype.

屬性

TypedArray.BYTES_PER_ELEMENT
Returns a number value of the element size for the different TypedArray objects.
TypedArray.length
Length property, whose value is 3.
TypedArray.name
Returns the string value of the constructor name (e.g, "Int8Array").
get TypedArray[@@species]
The constructor function used to create derived objects.
TypedArray.prototype
Prototype for TypedArray objects.

方法

TypedArray.from()
Creates a new TypedArray from an array-like or iterable object. See also Array.from().
TypedArray.of()
Creates a new TypedArray with a variable number of arguments. See also Array.of().

Methods Polyfill

Many of the methods used in Typed Arrays can be polyfilled using the methods present in regular Javascript Arrays. The following snippet of JavaScript demonstrates how you might polyfill any missing Typed Array methods.

var typedArrayTypes = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
          Uint16Array, ​​​Int32Array, Uint32Array, ​​​Float32Array, Float64Array];

for (var k in typedArrayTypes)
    for (var v in Array.prototype)
        if (Array.prototype.hasOwnProperty(v) && 
          !typedArrayTypes[k].prototype.hasOwnProperty(v))
            typedArrayTypes[k].prototype[v] = Array.prototype[v];

規範

Specification Status Comment
Typed Array Specification Obsolete Defined as TypedArray and ArrayBufferView interface with typed array view types. Superseded by ECMAScript 6.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'TypedArray Objects' in that specification.
Standard Initial definition in an ECMA standard. Specified behaviour for indexed and named properties. Specified that new is required.
ECMAScript Latest Draft (ECMA-262)
The definition of 'TypedArray Objects' in that specification.
Draft  

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
TypedArrayChrome Full support 7Edge Full support 12Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
BYTES_PER_ELEMENTChrome Full support 7Edge Full support 12Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
bufferChrome Full support 7Edge Full support 14Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
byteLengthChrome Full support 7Edge Full support 14Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
byteOffsetChrome Full support 7Edge Full support 14Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
Constructor without argumentsChrome Full support 7Edge Full support 12Firefox Full support 55IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 55Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android Full support 1.0nodejs ?
copyWithinChrome Full support 45Edge Full support 14Firefox Full support 34IE No support NoOpera Full support 36Safari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 34Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs Full support 4.0.0
entriesChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 36Safari No support NoWebView Android No support NoChrome Android Full support 45Firefox Android Full support 37Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 0.12
everyChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 36Safari No support NoWebView Android No support NoChrome Android Full support 45Firefox Android Full support 37Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
fillChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 36Safari No support NoWebView Android No support NoChrome Android Full support 45Firefox Android Full support 37Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
filterChrome Full support 45Edge Full support 14Firefox Full support 38IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android Full support 45Firefox Android Full support 38Opera Android No support NoSafari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
findChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 32Safari No support NoWebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 37Opera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
findIndexChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 32Safari No support NoWebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 37Opera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
forEachChrome Full support 45Edge Full support 14Firefox Full support 38IE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 38Opera Android Full support 32Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 4.0.0
fromChrome Full support 45Edge Full support 14Firefox Full support 38IE No support NoOpera No support NoSafari Full support 10WebView Android No support NoChrome Android No support NoFirefox Android Full support 38Opera Android No support NoSafari iOS Full support 10Samsung Internet Android No support Nonodejs Full support 4.0.0
includesChrome Full support 47Edge Full support 14Firefox Full support 43IE No support NoOpera Full support 34Safari Full support 10WebView Android No support NoChrome Android Full support 47Firefox Android Full support 43Opera Android Full support 34Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 6.0.0
Full support 6.0.0
Full support 5.0.0
Disabled
Disabled From version 5.0.0: this feature is behind the --harmony runtime flag.
Indexed properties not consulting prototypeChrome Full support 7
Notes
Full support 7
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
Edge Full support 12
Notes
Full support 12
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
Firefox Full support 25IE Full support 10
Notes
Full support 10
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
Opera Full support 11.6
Notes
Full support 11.6
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
Safari Full support 5.1
Notes
Full support 5.1
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
WebView Android Full support ≤37
Notes
Full support ≤37
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
Chrome Android Full support 18
Notes
Full support 18
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
Firefox Android Full support 25Opera Android Full support 12
Notes
Full support 12
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
Safari iOS Full support 5
Notes
Full support 5
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
Samsung Internet Android Full support 1.0
Notes
Full support 1.0
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
nodejs ?
Notes
?
Notes
Notes Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
indexOfChrome Full support 45Edge Full support 14Firefox Full support 37
Notes
Full support 37
Notes
Notes Starting with Firefox 47, this method will no longer return -0. For example, new Uint8Array([0]).indexOf(0, -0) will now always return +0.
IE No support NoOpera Full support 32Safari No support NoWebView Android No support NoChrome Android Full support 45Firefox Android Full support 37
Notes
Full support 37
Notes
Notes Starting with Firefox 47, this method will no longer return -0. For example, new Uint8Array([0]).indexOf(0, -0) will now always return +0.
Opera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
Iterable in constructorChrome Full support 39Edge Full support 14Firefox Full support 52IE No support NoOpera Full support 26Safari Full support 10WebView Android Full support 39Chrome Android Full support 39Firefox Android Full support 52Opera Android Full support 26Safari iOS Full support 10Samsung Internet Android Full support 4.0nodejs Full support 4.0.0
joinChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 32Safari No support NoWebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 37Opera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
keysChrome Full support 38Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 25Safari Full support 10WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 37Opera Android Full support 25Safari iOS Full support 10Samsung Internet Android Full support 3.0nodejs Full support 0.12
lastIndexOfChrome Full support 45Edge Full support 14Firefox Full support 37
Notes
Full support 37
Notes
Notes Starting with Firefox 47, this method will no longer return -0. For example, new Uint8Array([0]).lastIndexOf(0, -0) will now always return +0.
IE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 37
Notes
Full support 37
Notes
Notes Starting with Firefox 47, this method will no longer return -0. For example, new Uint8Array([0]).lastIndexOf(0, -0) will now always return +0.
Opera Android Full support 32Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 4.0.0
lengthChrome Full support 7Edge Full support 14Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
mapChrome Full support 45Edge Full support 14Firefox Full support 38IE No support NoOpera Full support 32Safari No support NoWebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 38Opera Android Full support 32Safari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
nameChrome Full support 7Edge Full support 12Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
Named propertiesChrome Full support 7Edge Full support 12Firefox Full support 30IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 30Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android Full support 1.0nodejs ?
TypedArray() without new throwsChrome Full support 7Edge Full support 14Firefox Full support 44IE No support NoOpera Full support 15Safari Full support 5.1WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 44Opera Android Full support 14Safari iOS Full support 5Samsung Internet Android Full support 1.0nodejs Full support 0.12
ofChrome Full support 45Edge Full support 14Firefox Full support 38IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 38Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs Full support 4.0.0
reduceChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 37Opera Android No support NoSafari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 4.0.0
reduceRightChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 37Opera Android No support NoSafari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 4.0.0
reverseChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 37Opera Android No support NoSafari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 4.0.0
setChrome Full support 7Edge Full support 14Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs ?
sliceChrome Full support 45Edge Full support 14Firefox Full support 38IE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 38Opera Android Full support 32Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 4.0.0
someChrome Full support 45Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 37Opera Android No support NoSafari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 4.0.0
sortChrome Full support 45Edge Full support 14Firefox Full support 46IE No support NoOpera Full support 32Safari Full support 10WebView Android Full support 45Chrome Android Full support 45Firefox Android Full support 46Opera Android Full support 32Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 4.0.0
subarrayChrome Full support 7Edge Full support 14Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support 0.12
toLocaleStringChrome Full support 7Edge Full support 12Firefox Full support 51IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 51Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android Full support 1.0nodejs ?
toStringChrome Full support 7Edge Full support 12Firefox Full support 51IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 51Opera Android Full support 12Safari iOS Full support 5Samsung Internet Android Full support 1.0nodejs ?
valuesChrome Full support 38Edge Full support 14Firefox Full support 37IE No support NoOpera Full support 25Safari Full support 10WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 37Opera Android Full support 25Safari iOS Full support 10Samsung Internet Android Full support 3.0nodejs Full support 0.12
@@iteratorChrome Full support 38Edge Full support 12Firefox Full support 36
Full support 36
No support 27 — 36
Notes Alternate Name
Notes A placeholder property named @@iterator is used.
Alternate Name Uses the non-standard name: @@iterator
No support 17 — 27
Notes Alternate Name
Notes A placeholder property named iterator is used.
Alternate Name Uses the non-standard name: iterator
IE No support NoOpera Full support 25Safari Full support 10WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 36
Full support 36
No support 27 — 36
Notes Alternate Name
Notes A placeholder property named @@iterator is used.
Alternate Name Uses the non-standard name: @@iterator
No support 17 — 27
Notes Alternate Name
Notes A placeholder property named iterator is used.
Alternate Name Uses the non-standard name: iterator
Opera Android Full support 25Safari iOS Full support 10Samsung Internet Android Full support 3.0nodejs Full support 0.12
@@speciesChrome Full support 51Edge Full support 13Firefox Full support 48IE No support NoOpera Full support 38Safari No support NoWebView Android Full support 51Chrome Android Full support 51Firefox Android Full support 48Opera Android Full support 41Safari iOS No support NoSamsung Internet Android Full support 5.0nodejs Full support 6.5.0
Full support 6.5.0
Full support 6.0.0
Disabled
Disabled From version 6.0.0: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.

相容性備註

Starting with ECMAScript 2015, TypedArray constructors require to be constructed with a new operator. Calling a TypedArray constructor as a function without new, will throw a TypeError from now on.

var dv = Int8Array([1, 2, 3]);
// TypeError: calling a builtin Int8Array constructor 
// without new is forbidden
var dv = new Int8Array([1, 2, 3]);

參見