# TypedArray
原始二进制缓冲区上的类似数组的视图。
¥An Array-like view on a raw binary buffer.
TypedArray API 的工作方式非常类似于 JavaScript (MDN (opens new window))。下面的名称 TypedArray 代表元素类型 T 的变体之一,并不是实际的类。请注意,类型化数组实际上并不是通用的,但像 Uint8Array 和 Float64Array 这样的具体实现具有相同的接口,仅在表示为 T 的实现元素类型方面有所不同。
¥The TypedArray API works very much like JavaScript's (MDN (opens new window)). The name TypedArray below represents one of the variants of element type T and is not an actual class. Note that typed arrays are not actually generic, but concrete implementations like Uint8Array and Float64Array have an identical interface that only differs in the implementation's element type denoted as T.
# 变体
¥Variants
| 变体 | 元素类型 | 描述 | 
|---|---|---|
| 整型 8 数组 | i8 | 8 位有符号整数值的视图。 | 
| Int16 数组 | i16 | 16 位有符号整数值的视图。 | 
| Int32 数组 | i32 | 32 位有符号整数值的视图。 | 
| Int64 数组 | i64 | 64 位有符号整数值的视图。 | 
| Uint8Array | u8 | 8 位无符号整数值的视图。 | 
| Uint8ClampedArray | u8 | 8 位无符号整数值的视图, 设置值夹在 0和255之间(包括0和255)。 | 
| Uint16 数组 | u16 | 16 位无符号整数值的视图。 | 
| Uint32Array | u32 | 32 位无符号整数值的视图。 | 
| Uint64Array | u64 | 64 位无符号整数值的视图。 | 
| 浮点 32 数组 | F32 | 32 位浮点值的视图。 | 
| Float64 数组 | f64 | 64 位浮点值的视图。 | 
# 构造函数
¥Constructor
- new TypedArray(length: i32)- 使用新的后备缓冲区构造一个新的类型化数组视图,并将所有值初始化为零。有关封装原始缓冲区的信息,请参阅下面的 - wrap。- ¥Constructs a new typed array view with a new backing buffer and all values initialized to zero. See - wrapbelow for wrapping a raw buffer.
# 静态成员
¥Static members
- const BYTES_PER_ELEMENT: usize- 每个元素的字节数。 - ¥Number of bytes per element. 
- function wrap(buffer: ArrayBuffer, byteOffset?: i32, length?: i32): TypedArray- 封装原始缓冲区,将其视为类型化数组值类型的值序列。这相当于 JS 中相应的替代构造函数签名,但存在是因为还没有函数重载。 - ¥Wraps a raw buffer to be viewed as a sequence of values of the typed array's value type. This is equivalent to the respective alternative constructor signature in JS but exists because there is no function overloading (yet). 
# 实例成员
¥Instance members
# 字段
¥Fields
- readonly buffer: ArrayBuffer- 该视图的后备数组缓冲区。 - ¥The backing array buffer of this view. 
- readonly byteOffset: i32- 距后备缓冲区开头的偏移量(以字节为单位)。 - ¥The offset in bytes from the start of the backing buffer. 
- readonly byteLength: i32- 从后备缓冲区开头算起的长度(以字节为单位)。 - ¥The length in bytes from the start of the backing buffer. 
- readonly length: i32- 元素的长度。 - ¥The length in elements. 
# 方法
¥Methods
- function every(fn: (value: T, index: i32, self: TypedArray) => bool): bool- 使用数组的每个值调用指定的函数,直到找到函数返回 - false的第一个值。如果所有函数都返回- true或数组为空,则返回- true,否则返回- false。- ¥Calls the specified function with every value of the array until it finds the first value for which the function returns - false. Returns- trueif all functions returned- trueor the array is empty, otherwise- false.
- function fill(value: T, start?: i32, end?: i32): this- 将数组中从 - start(含)到- end(不含)的值替换为指定值,返回该数组。- ¥Replaces the values of the array from - startinclusive to- endexclusive in place with the specified value, returning the array.
- function findIndex(fn: (value: T, index: i32, self: TypedArray) => bool): i32- 使用数组的每个值调用指定的函数,直到找到该函数返回 - true的第一个值,并返回其索引。如果情况并非如此,则返回- -1。- ¥Calls the specified function with every value of the array until it finds the first value for which the function returns - true, returning its index. Returns- -1if that's never the case.
- function findLastIndex(fn: (value: T, index: i32, self: TypedArray) => bool): i32- 使用从末尾开始的数组的每个值调用指定的函数,直到找到该函数返回 - true的第一个值,并返回其索引。如果情况并非如此,则返回- -1。- ¥Calls the specified function with every value of the array starting at the end until it finds the first value for which the function returns - true, returning its index. Returns- -1if that's never the case.
- function forEach(fn: (value: T, index: i32, self: TypedArray) => void): void- 使用数组的每个值调用指定的函数。 - ¥Calls the specified function with every value of the array. 
- function includes(value: T, fromIndex?: i32): bool- 测试数组是否包含指定值,可以选择提供起始索引。 - ¥Tests if the array includes the specified value, optionally providing a starting index. 
- function indexOf(value: T, fromIndex?: i32): i32- 获取可以在数组中找到指定值的第一个索引。如果未找到则返回 - -1。- ¥Gets the first index where the specified value can be found in the array. Returns - -1if not found.
- function lastIndexOf(value: T, fromIndex?: i32): i32- 获取可以在数组中找到指定值的最后一个索引。如果未找到则返回 - -1。- ¥Gets the last index where the specified value can be found in the array. Returns - -1if not found.
- function map(fn: (value: T, index: i32, self: TypedArray) => T): TypedArray- 使用数组的每个值调用指定的函数,返回函数返回值的新数组。 - ¥Calls the specified function with every value of the array, returning a new array of the function's return values. 
- function reduce<U>( fn: (accumValue: U, currentValue T, index: i32, self: TypedArray) => U, initialValue: U ): U- 使用数组的每个值调用指定的缩减器函数,从而产生单个返回值。各个前一个 reducer 函数的返回值被记住在 - accumValue中,从- initialValue开始,成为该过程中的最终返回值。- ¥Calls the specified reducer function with each value of the array, resulting in a single return value. The respective previous reducer function's return value is remembered in - accumValue, starting with- initialValue, becoming the final return value in the process.
- function reduceRight<U>( fn: (accumValue: U, currentValue: T, index: i32, self: TypedArray) => U, initialValue: U ): U- 使用数组的每个值从右到左调用指定的缩减器函数,从而产生单个返回值。 - ¥Calls the specified reducer function with each value of the array, from right to left, resulting in a single return value. 
- function reverse(): this- 就地反转数组的值,在返回数组之前修改数组。 - ¥Reverses an array's values in place, modifying the array before returning it. 
- function set(source: ArrayLike<number>, offset?: i32): void- 设置类型化数组值(从 - offset或 0 开始),从指定的- source数组读取输入值。- ¥Sets the typed array values (starting at - offset, or 0), reading input values from a specified- sourcearray.
- function some(fn: (value: T, index: i32, self: TypedArray) => bool): bool- 使用数组的每个值调用指定的函数,直到找到函数返回 - true的第一个值,然后返回- true。否则或如果数组为空,则返回- false。- ¥Calls the specified function with every value of the array until it finds the first value for which the function returns - true, returning- true. Returns- falseotherwise or if the array is empty.
- function sort(fn: (a: T, b: T) => i32): this- 使用指定的比较器函数对数组的值进行就地排序,并在返回数组之前修改数组。比较器返回负值表示 - a < b,返回正值表示- a > b和- 0表示两者相等。与 JavaScript 中执行到字符串的隐式转换不同,比较器默认比较两个- T类型的值。- ¥Sorts the values of the array in place, using the specified comparator function, modifying the array before returning it. The comparator returning a negative value means - a < b, a positive value means- a > band- 0means that both are equal. Unlike in JavaScript, where an implicit conversion to strings is performed, the comparator defaults to compare two values of type- T.
- function subarray(start?: i32, end?: i32): TypedArray- 返回数组后备缓冲区的新视图,相对于该数组,从 - begin(含)到- end(不含)。如果省略,- end默认为数组末尾。不复制。- ¥Returns a new view on the array's backing buffer from - begininclusive to- endexclusive relative to this array. If omitted,- enddefaults to the end of the array. Does not copy.