TypedArray

这篇翻译不完整。请帮忙从英语翻译这篇文章

TypedArray 对象表示底层的二进制数据缓存区的类数组视图. 没有名为 TypedArray 的全局属性,也不存在直接可见的 TypedArray 构造器。相反,有若干不同的全局属性的值是类型数组的特殊构造器(typed array constructors for specific element types),用于特定的元素类型,这些在下文有列出。接下来,会介绍能和任何包含任意类型元素的类型数组一起使用的通用属性和方法。

语法

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

以下皆是 TypedArray() : 

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

参数

length
当传入 length 作为参数时,将会创建一个包含 length 个 0 的类型数组。
typedArray
当传入一个任意类型的类型数组对象 typedArray (比如 Int32Array)作为参数时,这个 typedArray 会被复制到新的数组。在原 typedArray 中的每个值都会被转化为和构造器相对应的类型到新的数组里。
object
当传入一个 object 作为参数时,如同通过 TypedArray.from() 方法一样创建一个新的类型数组。
buffer, byteOffset, length
当传入一个 buffer 和可选参数 byteOffsetlength,将会创建一个新的类型数组视图(参考 ArrayBuffer )。byteOffset 和 length 参数规定了改类型数组视图的可视的内存范围(the memory range that will be exposed by the typed array view)。如果这两个可选参数都被省略,那么全部的缓冲区都是可视的(all of buffer is viewed); 如果仅仅省略了 length , 那么余下的缓冲区是可视的(即偏移量之后的)。

描述

ECMAScript 6 defines a TypedArray constructor that serves as the [[Prototype]] of allTypedArray constructors.  This constructor is not directly exposed: there is no global%TypedArray% or TypedArray property.  It is only directly accessible throughObject.getPrototypeOf(Int8Array.prototype) 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.

属性访问

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 objects

类型 大小(字节单位) 描述 Web IDL type C语言中的等效类型
Int8Array 1 8-bit twos complement signed integer byte int8_t
Uint8Array 1 8-bit unsigned integer octet uint8_t
Int16Array 2 16-bit twos complement signed integer short int16_t
Uint16Array 2 16-bit unsigned integer unsigned short uint16_t
Int32Array 4 32-bit twos complement signed integer long int32_t
Uint32Array 4 32-bit unsigned integer unsigned int uint32_t
Float32Array 4 32-bit IEEE floating point number unrestricted float float
Float64Array 8 64-bit IEEE floating point number 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 3.
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().

Specifications

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)
TypedArray Objects
Standard Initial definition in an ECMA standard. Specified behaviour for indexed and named properties. Specified that new is required.
ECMAScript 2017 Draft (ECMA-262)
TypedArray Objects
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 7.0 4.0 (2) 10 11.6 5.1
Indexed properties not consulting prototype (Yes) [1] 25 (25) ? ? ?
Named properties (Yes) 30 (30) ? ? ?
new is required ? 44 (44) ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support 4.0 (Yes) 4.0 (2) 10 11.6 4.2 (Yes)
Indexed properties not consulting prototype ? (Yes) [1] 25.0 (25) ? ? ? (Yes) [1]
Named properties ? (Yes) 30.0 (30) ? ? ? ?
new is required ? ? 44.0 (44) ? ? ? ?

[1] -1 and similar are not considered as indexed properties and therefore return the value of the prototype property.

Compatibility notes

Starting with ECMAScript 2015 (ES6), 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]);

相关链接

文档标签和贡献者

 此页面的贡献者: Ende93, teoli, David_Li
 最后编辑者: Ende93,