Uint8ClampedArray

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Uint8ClampedArray(8 位无符号整型固定数组) 类型化数组表示一个由值固定在 0-255 区间的 8 位无符号整型组成的数组;如果你指定一个在 [0,255] 区间外的值,它将被替换为 0 或 255;如果你指定一个非整数,那么它将被设置为最接近它的整数。(数组)内容被初始化为 0。一旦(数组)被创建,你可以使用对象的方法引用数组里的元素,或使用标准的数组索引语法(即使用方括号标记)。

语法

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

关于构造函数语法和参数的更多信息,参见 TypedArray

属性

Uint8ClampedArray.BYTES_PER_ELEMENT

返回元素大小的一个数值。对 Uint8ClampedArray 而言是 1。

Uint8ClampedArray.prototype.length

静态长度属性值为 0。对于实际长度(元素的数量),见 Uint8ClampedArray.prototype.length

Uint8ClampedArray.prototype (en-US)

原型是 TypedArray(类型化数组)对象。

方法

Uint8ClampedArray.from()

从一个类数组或可枚举对象创建一个新的 Uint8ClampedArray。参见 Array.from()

Uint8ClampedArray.of()

通过一个可选数量参数来创建一个新的 Uint8ClampedArray。参见 Array.of()

Uint8ClampedArray 原型

所有的 Uint8ClampedArray 对象继承自 %TypedArray%.prototype (en-US)

属性

Uint8ClampedArray.prototype.constructor

返回创建一个实例原型的函数。这是 Uint8ClampedArray 默认的构造函数。

Uint8ClampedArray.prototype.buffer 只读

返回由 Uint8ClampedArray 引用的 ArrayBuffer 。在创建时所固定下来,因此只能读取

Uint8ClampedArray.prototype.byteLength 只读

返回从 ArrayBuffer 开始的 Uint8ClampedArray 的(字节的)长度。在创建时所固定下来,因此只能读取

Uint8ClampedArray.prototype.byteOffset 只读

返回从 ArrayBuffer 开始的 Uint8ClampedArray 的(字节的)偏移。在创建时所固定下来,因此只能读取

Uint8ClampedArray.prototype.length 只读

返回 UintClamped8Array 具有的元素数量。在创建时所固定下来,因此只能读取

方法

Uint8ClampedArray.prototype.copyWithin()

复制数组内一段数组元素的序列。参见 Array.prototype.copyWithin()

Uint8ClampedArray.prototype.entries()

返回一个新的包含数组中每个索引对应的键/值对的数组迭代器对象。参见 Array.prototype.entries()

Uint8ClampedArray.prototype.every()

测试数组里的所有元素是否通过所提供的函数的测试。参见 Array.prototype.every()

Uint8ClampedArray.prototype.fill()

用一个固定值填充一个数组内的从起始索引到结束索引的全部元素。参见 Array.prototype.fill()

Uint8ClampedArray.prototype.filter()

由该数组中所有经所提供的筛选函数返回为 true 的元素创建一个新数组。参见 Array.prototype.filter()

Uint8ClampedArray.prototype.find()

如果数组里的一个元素符合所提供的测试函数则返回找到的这个值,如果没有找到则返回 undefined。参见 Array.prototype.find()

Uint8ClampedArray.prototype.findIndex()

如果数组里的一个元素符合所提供的测试函数则返回找到的索引,如果没有找到则返回 -1。参见 Array.prototype.findIndex()

Uint8ClampedArray.prototype.forEach()

对数组内的每个元素调用一个函数。参见 Array.prototype.forEach()

Uint8ClampedArray.prototype.includes() 实验性

确定一个类型化数组是否包含一个特定的元素,对应地返回 truefalse。参见 Array.prototype.includes()

Uint8ClampedArray.prototype.indexOf()

Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found. See also Array.prototype.indexOf().

Uint8ClampedArray.prototype.join()

Joins all elements of an array into a string. See also Array.prototype.join().

Uint8ClampedArray.prototype.keys()

Returns a new Array Iterator that contains the keys for each index in the array. See also Array.prototype.keys().

Uint8ClampedArray.prototype.lastIndexOf()

Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found. See also Array.prototype.lastIndexOf().

Uint8ClampedArray.prototype.map()

Creates a new array with the results of calling a provided function on every element in this array. See also Array.prototype.map().

Uint8ClampedArray.prototype.move() 非标准

Former non-standard version of Uint8ClampedArray.prototype.copyWithin().

Uint8ClampedArray.prototype.reduce()

Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value. See also Array.prototype.reduce().

Uint8ClampedArray.prototype.reduceRight()

Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value. See also Array.prototype.reduceRight().

Uint8ClampedArray.prototype.reverse()

Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first. See also Array.prototype.reverse().

Uint8ClampedArray.prototype.set()

Stores multiple values in the typed array, reading input values from a specified array.

Uint8ClampedArray.prototype.slice()

Extracts a section of an array and returns a new array. See also Array.prototype.slice().

Uint8ClampedArray.prototype.some()

Returns true if at least one element in this array satisfies the provided testing function. See also Array.prototype.some().

Uint8ClampedArray.prototype.sort()

Sorts the elements of an array in place and returns the array. See also Array.prototype.sort().

Uint8ClampedArray.prototype.subarray()

Returns a new Uint8ClampedArray from the given start and end element index.

Uint8ClampedArray.prototype.values()

Returns a new Array Iterator object that contains the values for each index in the array. See also Array.prototype.values().

Uint8ClampedArray.prototype.toLocaleString()

Returns a localized string representing the array and its elements. See also Array.prototype.toLocaleString().

Uint8ClampedArray.prototype.toString()

Returns a string representing the array and its elements. See also Array.prototype.toString().

Uint8ClampedArray.prototype[@@iterator]()

Returns a new Array Iterator object that contains the values for each index in the array.

示例

创建一个 Uint8ClampedArray 的不同方式:

js
// From a length
var uintc8 = new Uint8ClampedArray(2);
uintc8[0] = 42;
uintc8[1] = 1337;
console.log(uintc8[0]); // 42
console.log(uintc8[1]); // 255 (clamped)
console.log(uintc8.length); // 2
console.log(uintc8.BYTES_PER_ELEMENT); // 1

// From an array
var arr = new Uint8ClampedArray([21, 31]);
console.log(arr[1]); // 31

// From another TypedArray
var x = new Uint8ClampedArray([21, 31]);
var y = new Uint8ClampedArray(x);
console.log(y[0]); // 21

// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint8ClampedArray(buffer, 1, 4);

// From an iterable
var iterable = (function* () {
  yield* [1, 2, 3];
})();
var uintc8 = new Uint8ClampedArray(iterable);
// Uint8ClampedArray[1, 2, 3]

规范

Specification
ECMAScript Language Specification
# table-49

浏览器兼容性

BCD tables only load in the browser

兼容性注意事项

从 ECMAScript 2015 开始, Uint8ClampedArray 构造函数需要用一个 new 操作符来构建。从现在开始,不使用 new 来调用一个 Uint8ClampedArray 构造函数将会抛出一个 TypeError

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

参见